{"id":512,"date":"2026-09-29T10:00:00","date_gmt":"2026-09-29T08:00:00","guid":{"rendered":"https:\/\/www.lukaswojcik.com\/?page_id=512"},"modified":"2026-11-15T12:06:18","modified_gmt":"2026-11-15T11:06:18","slug":"building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses","status":"publish","type":"post","link":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/","title":{"rendered":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses"},"content":{"rendered":"<h2>A small endpoint with clearly defined response fields<\/h2>\n<p>A REST endpoint is an address that a program can call to request data. A custom WordPress endpoint can expose exactly the fields needed by a page component, such as a list of recent posts. The benefit is a predictable interface and a bounded workload. A switch from <code>admin-ajax.php<\/code> does not by itself guarantee a particular response time.<\/p>\n<p>Both request paths load WordPress and relevant plugin and theme code. <code>admin-ajax.php<\/code> additionally loads administration code and runs <code>admin_init<\/code>. The cost depends on the installed callbacks, database, cache and server load. \u201c400\u2013600 ms versus under 50 ms\u201d and \u201c60\u201380% faster\u201d require measurements; those numbers are not properties of REST.<\/p>\n<figure class=\"lw-diagram\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/diagrams\/dia-512-rest-20261024.png?v=20261115\" width=\"1120\" height=\"580\" alt=\"Explicit access rules, bounded inputs and text output come before performance measurements.\"><figcaption>Explicit access rules, bounded inputs and text output come before performance measurements.<\/figcaption><\/figure>\n<h2>1. Existing API or custom route<\/h2>\n<p>For a normal list of posts, <code>wp\/v2\/posts<\/code> already provides a maintained endpoint. A limited <code>per_page<\/code> and selected <code>_fields<\/code> can reduce the response. A custom route is useful when the existing contract does not fit. Extra routes also need access rules, tests and maintenance.<\/p>\n<p>The example below returns recent <strong>post metadata<\/strong>, not logs. It is a demonstration for a site where the selected published, non-password-protected post titles and dates are intentionally public. Membership restrictions, language selection and other visibility rules need integration-specific checks before public exposure. Neither <code>WP_Query<\/code> nor a successful permission callback automatically implements every plugin\u2019s access policy.<\/p>\n\n\n<!-- lw-flux-illu 512-a2 -->\n<figure class=\"lw-illu\"><img src=\"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/illustrationen\/flux-512-a2.webp\" width=\"1120\" height=\"630\" loading=\"lazy\" decoding=\"async\" alt=\"At a stream intake, a grate holds back branches and leaves, and a throttle plate limits how much water flows on. Likewise, a request route is registered, its inputs are checked and its use is limited.\"><figcaption>At a stream intake, a grate holds back branches and leaves, and a throttle plate limits how much water flows on. Likewise, a request route is registered, its inputs are checked and its use is limited.<\/figcaption><\/figure>\n\n\n<h2>2. Request registration, validation and limits<\/h2>\n<p>Registration belongs on <code>rest_api_init<\/code>. A namespace such as <code>lw-toolbox\/v1<\/code> separates the route from other plugins. Registering elsewhere is unsupported usage and can trigger a warning; an automatic 404 in every such case is not guaranteed.<\/p>\n<p>The <code>limit<\/code> rule accepts only ordinary decimal integers from 1 through 100. Fractions, scientific notation, arrays, zero and negative values are rejected. Validation decides what is accepted; conversion then produces an integer. Validation is not authorization or a substitute for parameterized SQL.<\/p>\n<figure style=\"--cbb-digits:2\" class=\"cbb cbb--wrap cbb--numbers wp-block-code-beautifier-block-code\" data-lang=\"php\"><figcaption class=\"cbb-head\"><span class=\"cbb-lang\">PHP<\/span><span class=\"cbb-actions\"><button type=\"button\" class=\"cbb-button cbb-button--wrap\" data-cbb-action=\"wrap\" aria-pressed=\"true\" title=\"Wrap long lines\"><svg aria-hidden=\"true\" focusable=\"false\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><path d=\"M2 4h12M2 8h9.5a2.5 2.5 0 0 1 0 5H8m0 0 1.5-1.5M8 13l1.5 1.5M2 12h3\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"\/><\/svg><span class=\"cbb-sr\">Wrap long lines<\/span><\/button><button type=\"button\" class=\"cbb-button cbb-button--copy\" data-cbb-action=\"copy\" aria-label=\"Copy code\" data-cbb-status-done=\"Code copied to the clipboard.\" data-cbb-status-failed=\"Copying failed. The code is selected for copying with Ctrl+C or \u2318+C.\" data-cbb-status-noselect=\"Copying failed.\"><svg aria-hidden=\"true\" focusable=\"false\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><rect x=\"5.5\" y=\"5.5\" width=\"8\" height=\"8.5\" rx=\"1.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"\/><path d=\"M10.5 3.5V3A1.5 1.5 0 0 0 9 1.5H4A1.5 1.5 0 0 0 2.5 3v6.5A1.5 1.5 0 0 0 4 11h.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"\/><\/svg><span class=\"cbb-button-label\">Copy<\/span><\/button><\/span><\/figcaption><pre class=\"cbb-pre\" tabindex=\"0\" role=\"region\" aria-label=\"Code example: PHP\"><code class=\"cbb-code language-php\" style=\"counter-reset:cbb-line 0;\"><span class=\"cbb-line\"><span class=\"cbb-function\">add_action<\/span>( <span class=\"cbb-string\">'rest_api_init'<\/span>, <span class=\"cbb-keyword\">function<\/span> () {\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-function\">register_rest_route<\/span>( <span class=\"cbb-string\">'lw-toolbox\/v1'<\/span>, <span class=\"cbb-string\">'\/latest-posts'<\/span>, <span class=\"cbb-keyword\">array<\/span>(\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-string\">'methods'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-type\">WP_REST_Server<\/span><span class=\"cbb-operator\">::<\/span><span class=\"cbb-constant\">READABLE<\/span>,\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-comment\">\/\/ Public data only: no private logs or membership content.<\/span>\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-string\">'permission_callback'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-string\">'__return_true'<\/span>,\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-string\">'args'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-keyword\">array<\/span>(\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-string\">'limit'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-keyword\">array<\/span>(\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'default'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-number\">10<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'validate_callback'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-keyword\">function<\/span> ( <span class=\"cbb-variable\">$value<\/span> ) {\n<\/span><span class=\"cbb-line\">                    <span class=\"cbb-keyword\">return<\/span> ( <span class=\"cbb-function\">is_string<\/span>( <span class=\"cbb-variable\">$value<\/span> ) || <span class=\"cbb-function\">is_int<\/span>( <span class=\"cbb-variable\">$value<\/span> ) )\n<\/span><span class=\"cbb-line\">                        &amp;&amp; <span class=\"cbb-function\">preg_match<\/span>( <span class=\"cbb-string\">'\/\\A(?:&#91;1-9&#93;&#91;0-9&#93;?|100)\\z\/'<\/span>, <span class=\"cbb-type\">(string)<\/span> <span class=\"cbb-variable\">$value<\/span> ) === <span class=\"cbb-number\">1<\/span>;\n<\/span><span class=\"cbb-line\">                },\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'sanitize_callback'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-keyword\">function<\/span> ( <span class=\"cbb-variable\">$value<\/span> ) {\n<\/span><span class=\"cbb-line\">                    <span class=\"cbb-keyword\">return<\/span> <span class=\"cbb-type\">(int)<\/span> <span class=\"cbb-variable\">$value<\/span>;\n<\/span><span class=\"cbb-line\">                },\n<\/span><span class=\"cbb-line\">            ),\n<\/span><span class=\"cbb-line\">        ),\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-string\">'callback'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-keyword\">function<\/span> ( WP_REST_Request <span class=\"cbb-variable\">$request<\/span> ) {\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-variable\">$query<\/span> = <span class=\"cbb-keyword\">new<\/span> <span class=\"cbb-type\">WP_Query<\/span>( <span class=\"cbb-keyword\">array<\/span>(\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'post_type'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-string\">'post'<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'post_status'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-string\">'publish'<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'has_password'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-constant\">false<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'posts_per_page'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-variable\">$request<\/span><span class=\"cbb-operator\">-&gt;<\/span><span class=\"cbb-function\">get_param<\/span>( <span class=\"cbb-string\">'limit'<\/span> ),\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'orderby'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-keyword\">array<\/span>( <span class=\"cbb-string\">'date'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-string\">'DESC'<\/span>, <span class=\"cbb-string\">'ID'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-string\">'DESC'<\/span> ),\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'ignore_sticky_posts'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-constant\">true<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'no_found_rows'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-constant\">true<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'update_post_meta_cache'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-constant\">false<\/span>,\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-string\">'update_post_term_cache'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-constant\">false<\/span>,\n<\/span><span class=\"cbb-line\">            ) );\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-variable\">$items<\/span> = <span class=\"cbb-keyword\">array<\/span>();\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-keyword\">foreach<\/span> ( <span class=\"cbb-variable\">$query<\/span><span class=\"cbb-operator\">-&gt;<\/span>posts <span class=\"cbb-keyword\">as<\/span> <span class=\"cbb-variable\">$post<\/span> ) {\n<\/span><span class=\"cbb-line\">                <span class=\"cbb-variable\">$items<\/span>&#91;&#93; = <span class=\"cbb-keyword\">array<\/span>(\n<\/span><span class=\"cbb-line\">                    <span class=\"cbb-string\">'id'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-type\">(int)<\/span> <span class=\"cbb-variable\">$post<\/span><span class=\"cbb-operator\">-&gt;<\/span>ID,\n<\/span><span class=\"cbb-line\">                    <span class=\"cbb-string\">'title'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-type\">(string)<\/span> <span class=\"cbb-variable\">$post<\/span><span class=\"cbb-operator\">-&gt;<\/span>post_title,\n<\/span><span class=\"cbb-line\">                    <span class=\"cbb-string\">'date_gmt'<\/span> <span class=\"cbb-operator\">=&gt;<\/span> <span class=\"cbb-type\">(string)<\/span> <span class=\"cbb-variable\">$post<\/span><span class=\"cbb-operator\">-&gt;<\/span>post_date_gmt,\n<\/span><span class=\"cbb-line\">                );\n<\/span><span class=\"cbb-line\">            }\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-variable\">$response<\/span> = <span class=\"cbb-keyword\">new<\/span> <span class=\"cbb-type\">WP_REST_Response<\/span>( <span class=\"cbb-variable\">$items<\/span>, <span class=\"cbb-number\">200<\/span> );\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-variable\">$response<\/span><span class=\"cbb-operator\">-&gt;<\/span><span class=\"cbb-function\">header<\/span>( <span class=\"cbb-string\">'Cache-Control'<\/span>, <span class=\"cbb-string\">'no-store'<\/span> );\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-keyword\">return<\/span> <span class=\"cbb-variable\">$response<\/span>;\n<\/span><span class=\"cbb-line\">        },\n<\/span><span class=\"cbb-line\">    ) );\n<\/span><span class=\"cbb-line\">} );<\/span><\/code><\/pre><span class=\"cbb-status\" role=\"status\"><\/span><\/figure>\n<p>The code belongs in a small site-specific plugin or an appropriate child-theme file; a new PHP file needs its opening PHP tag. The public permission callback is a deliberate access decision. Private logs, account data and restricted content require an appropriate capability or object-level check instead.<\/p>\n<p><code>no_found_rows<\/code> avoids an unnecessary total-count query; metadata and term-cache priming are disabled because the response does not use those fields. Ordinary query caching remains available. <code>WP_Query<\/code> is not inherently slow, and direct SQL is not inherently faster. Direct SQL can bypass query filters and plugin visibility rules; where a custom table genuinely requires SQL, values belong in <code>$wpdb-&gt;prepare()<\/code> placeholders and identifiers must come from trusted configuration.<\/p>\n<p>The example supplies a compact success response, including an empty list when no posts match. It is not a complete monitoring system: database failures must be observable through restricted server monitoring and a deliberate error policy. Error responses must not expose database statements or credentials. A rejected REST parameter prevents this callback\u2019s query, not all database work performed while WordPress starts.<\/p>\n<h2>3. The endpoint URL generated by WordPress<\/h2>\n<p>A hard-coded host and root-level <code>\/wp-json\/<\/code> path can fail on a subdirectory installation or another permalink configuration. The following container belongs in a PHP template, not an ordinary HTML block in the editor. <code>rest_url()<\/code> derives the URL; <code>esc_url()<\/code> escapes the HTML attribute. The three visible state messages should match the page language.<\/p>\n<figure style=\"--cbb-digits:1\" class=\"cbb cbb--wrap cbb--numbers wp-block-code-beautifier-block-code\" data-lang=\"php\"><figcaption class=\"cbb-head\"><span class=\"cbb-lang\">PHP<\/span><span class=\"cbb-actions\"><button type=\"button\" class=\"cbb-button cbb-button--wrap\" data-cbb-action=\"wrap\" aria-pressed=\"true\" title=\"Wrap long lines\"><svg aria-hidden=\"true\" focusable=\"false\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><path d=\"M2 4h12M2 8h9.5a2.5 2.5 0 0 1 0 5H8m0 0 1.5-1.5M8 13l1.5 1.5M2 12h3\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"\/><\/svg><span class=\"cbb-sr\">Wrap long lines<\/span><\/button><button type=\"button\" class=\"cbb-button cbb-button--copy\" data-cbb-action=\"copy\" aria-label=\"Copy code\" data-cbb-status-done=\"Code copied to the clipboard.\" data-cbb-status-failed=\"Copying failed. The code is selected for copying with Ctrl+C or \u2318+C.\" data-cbb-status-noselect=\"Copying failed.\"><svg aria-hidden=\"true\" focusable=\"false\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><rect x=\"5.5\" y=\"5.5\" width=\"8\" height=\"8.5\" rx=\"1.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"\/><path d=\"M10.5 3.5V3A1.5 1.5 0 0 0 9 1.5H4A1.5 1.5 0 0 0 2.5 3v6.5A1.5 1.5 0 0 0 4 11h.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"\/><\/svg><span class=\"cbb-button-label\">Copy<\/span><\/button><\/span><\/figcaption><pre class=\"cbb-pre\" tabindex=\"0\" role=\"region\" aria-label=\"Code example: PHP\"><code class=\"cbb-code language-php\" style=\"counter-reset:cbb-line 0;\"><span class=\"cbb-line\"><span class=\"cbb-punct\">&lt;<\/span><span class=\"cbb-tag\">div<\/span> <span class=\"cbb-attr\">id<\/span><span class=\"cbb-punct\">=<\/span><span class=\"cbb-string\">\"lw-fast-data-box\"<\/span> <span class=\"cbb-attr\">role<\/span><span class=\"cbb-punct\">=<\/span><span class=\"cbb-string\">\"status\"<\/span>\n<\/span><span class=\"cbb-line\">     <span class=\"cbb-attr\">data-endpoint<\/span><span class=\"cbb-punct\">=<\/span><span class=\"cbb-string\">\"&lt;?php echo esc_url( rest_url( 'lw-toolbox\/v1\/latest-posts' ) ); ?&gt;\"<\/span>\n<\/span><span class=\"cbb-line\">     <span class=\"cbb-attr\">data-loading<\/span><span class=\"cbb-punct\">=<\/span><span class=\"cbb-string\">\"Loading...\"<\/span> <span class=\"cbb-attr\">data-empty<\/span><span class=\"cbb-punct\">=<\/span><span class=\"cbb-string\">\"No posts.\"<\/span>\n<\/span><span class=\"cbb-line\">     <span class=\"cbb-attr\">data-error<\/span><span class=\"cbb-punct\">=<\/span><span class=\"cbb-string\">\"Posts could not be loaded.\"<\/span><span class=\"cbb-punct\">&gt;<\/span><span class=\"cbb-punct\">&lt;\/<\/span><span class=\"cbb-tag\">div<\/span><span class=\"cbb-punct\">&gt;<\/span><\/span><\/code><\/pre><span class=\"cbb-status\" role=\"status\"><\/span><\/figure>\n<h2>4. Data displayed as text<\/h2>\n<p>The JavaScript can be loaded once as an external frontend script. It handles loading, empty results, HTTP errors, malformed data and a ten-second client timeout. The public request intentionally omits credentials. No request body is sent, so a JSON <code>Content-Type<\/code> request header is unnecessary.<\/p>\n<figure style=\"--cbb-digits:2\" class=\"cbb cbb--wrap cbb--numbers wp-block-code-beautifier-block-code\" data-lang=\"javascript\"><figcaption class=\"cbb-head\"><span class=\"cbb-lang\">JavaScript<\/span><span class=\"cbb-actions\"><button type=\"button\" class=\"cbb-button cbb-button--wrap\" data-cbb-action=\"wrap\" aria-pressed=\"true\" title=\"Wrap long lines\"><svg aria-hidden=\"true\" focusable=\"false\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><path d=\"M2 4h12M2 8h9.5a2.5 2.5 0 0 1 0 5H8m0 0 1.5-1.5M8 13l1.5 1.5M2 12h3\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"\/><\/svg><span class=\"cbb-sr\">Wrap long lines<\/span><\/button><button type=\"button\" class=\"cbb-button cbb-button--copy\" data-cbb-action=\"copy\" aria-label=\"Copy code\" data-cbb-status-done=\"Code copied to the clipboard.\" data-cbb-status-failed=\"Copying failed. The code is selected for copying with Ctrl+C or \u2318+C.\" data-cbb-status-noselect=\"Copying failed.\"><svg aria-hidden=\"true\" focusable=\"false\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\"><rect x=\"5.5\" y=\"5.5\" width=\"8\" height=\"8.5\" rx=\"1.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"\/><path d=\"M10.5 3.5V3A1.5 1.5 0 0 0 9 1.5H4A1.5 1.5 0 0 0 2.5 3v6.5A1.5 1.5 0 0 0 4 11h.5\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"\/><\/svg><span class=\"cbb-button-label\">Copy<\/span><\/button><\/span><\/figcaption><pre class=\"cbb-pre\" tabindex=\"0\" role=\"region\" aria-label=\"Code example: JavaScript\"><code class=\"cbb-code language-javascript\" style=\"counter-reset:cbb-line 0;\"><span class=\"cbb-line\"><span class=\"cbb-keyword\">async<\/span> <span class=\"cbb-keyword\">function<\/span> <span class=\"cbb-function\">loadLatestPosts<\/span>() {\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-keyword\">const<\/span> box = document.<span class=\"cbb-function\">getElementById<\/span>(<span class=\"cbb-string\">'lw-fast-data-box'<\/span>);\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-keyword\">if<\/span> (!box) <span class=\"cbb-keyword\">return<\/span>;\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-keyword\">const<\/span> controller = <span class=\"cbb-keyword\">new<\/span> <span class=\"cbb-function\">AbortController<\/span>();\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-keyword\">const<\/span> timeout = <span class=\"cbb-function\">setTimeout<\/span>(() <span class=\"cbb-operator\">=&gt;<\/span> controller.<span class=\"cbb-function\">abort<\/span>(), <span class=\"cbb-number\">10000<\/span>);\n<\/span><span class=\"cbb-line\">    box.textContent = box.dataset.loading;\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-keyword\">try<\/span> {\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">const<\/span> url = <span class=\"cbb-keyword\">new<\/span> <span class=\"cbb-function\">URL<\/span>(box.dataset.endpoint, document.baseURI);\n<\/span><span class=\"cbb-line\">        url.searchParams.<span class=\"cbb-keyword\">set<\/span>(<span class=\"cbb-string\">'limit'<\/span>, <span class=\"cbb-string\">'5'<\/span>);\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">const<\/span> response = <span class=\"cbb-keyword\">await<\/span> <span class=\"cbb-function\">fetch<\/span>(url, {\n<\/span><span class=\"cbb-line\">            method: <span class=\"cbb-string\">'GET'<\/span>, credentials: <span class=\"cbb-string\">'omit'<\/span>, cache: <span class=\"cbb-string\">'no-store'<\/span>,\n<\/span><span class=\"cbb-line\">            headers: { <span class=\"cbb-type\">Accept<\/span>: <span class=\"cbb-string\">'application\/json'<\/span> },\n<\/span><span class=\"cbb-line\">            signal: controller.signal\n<\/span><span class=\"cbb-line\">        });\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">if<\/span> (!response.ok) <span class=\"cbb-keyword\">throw<\/span> <span class=\"cbb-keyword\">new<\/span> <span class=\"cbb-function\">Error<\/span>(<span class=\"cbb-string\">'HTTP '<\/span> + response.status);\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">const<\/span> items = <span class=\"cbb-keyword\">await<\/span> response.<span class=\"cbb-function\">json<\/span>();\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">if<\/span> (!<span class=\"cbb-type\">Array<\/span>.<span class=\"cbb-function\">isArray<\/span>(items) || items.length &gt; <span class=\"cbb-number\">5<\/span> || items.<span class=\"cbb-function\">some<\/span>(item <span class=\"cbb-operator\">=&gt;<\/span>\n<\/span><span class=\"cbb-line\">            !item || !<span class=\"cbb-type\">Number<\/span>.<span class=\"cbb-function\">isInteger<\/span>(item.id) ||\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-keyword\">typeof<\/span> item.title !== <span class=\"cbb-string\">'string'<\/span> || <span class=\"cbb-keyword\">typeof<\/span> item.date_gmt !== <span class=\"cbb-string\">'string'<\/span>\n<\/span><span class=\"cbb-line\">        )) <span class=\"cbb-keyword\">throw<\/span> <span class=\"cbb-keyword\">new<\/span> <span class=\"cbb-function\">Error<\/span>(<span class=\"cbb-string\">'Invalid response'<\/span>);\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">if<\/span> (!items.length) {\n<\/span><span class=\"cbb-line\">            box.textContent = box.dataset.empty;\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-keyword\">return<\/span>;\n<\/span><span class=\"cbb-line\">        }\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">const<\/span> list = document.<span class=\"cbb-function\">createElement<\/span>(<span class=\"cbb-string\">'ul'<\/span>);\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-keyword\">for<\/span> (<span class=\"cbb-keyword\">const<\/span> item <span class=\"cbb-keyword\">of<\/span> items) {\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-keyword\">const<\/span> li = document.<span class=\"cbb-function\">createElement<\/span>(<span class=\"cbb-string\">'li'<\/span>);\n<\/span><span class=\"cbb-line\">            <span class=\"cbb-keyword\">const<\/span> title = document.<span class=\"cbb-function\">createElement<\/span>(<span class=\"cbb-string\">'strong'<\/span>);\n<\/span><span class=\"cbb-line\">            title.textContent = item.title;\n<\/span><span class=\"cbb-line\">            li.<span class=\"cbb-function\">append<\/span>(title, document.<span class=\"cbb-function\">createTextNode<\/span>(<span class=\"cbb-string\">' ('<\/span> + item.date_gmt + <span class=\"cbb-string\">' UTC)'<\/span>));\n<\/span><span class=\"cbb-line\">            list.<span class=\"cbb-function\">append<\/span>(li);\n<\/span><span class=\"cbb-line\">        }\n<\/span><span class=\"cbb-line\">        box.<span class=\"cbb-function\">replaceChildren<\/span>(list);\n<\/span><span class=\"cbb-line\">    } <span class=\"cbb-keyword\">catch<\/span> (error) {\n<\/span><span class=\"cbb-line\">        box.textContent = box.dataset.error;\n<\/span><span class=\"cbb-line\">    } <span class=\"cbb-keyword\">finally<\/span> {\n<\/span><span class=\"cbb-line\">        <span class=\"cbb-function\">clearTimeout<\/span>(timeout);\n<\/span><span class=\"cbb-line\">    }\n<\/span><span class=\"cbb-line\">}\n<\/span><span class=\"cbb-line\"><span class=\"cbb-keyword\">if<\/span> (document.readyState === <span class=\"cbb-string\">'loading'<\/span>) {\n<\/span><span class=\"cbb-line\">    document.<span class=\"cbb-function\">addEventListener<\/span>(<span class=\"cbb-string\">'DOMContentLoaded'<\/span>, loadLatestPosts, { once: <span class=\"cbb-constant\">true<\/span> });\n<\/span><span class=\"cbb-line\">} <span class=\"cbb-keyword\">else<\/span> {\n<\/span><span class=\"cbb-line\">    <span class=\"cbb-function\">loadLatestPosts<\/span>();\n<\/span><span class=\"cbb-line\">}<\/span><\/code><\/pre><span class=\"cbb-status\" role=\"status\"><\/span><\/figure>\n<p><code>textContent<\/code> treats a title as text, including angle brackets. Interpolating a database title into <code>innerHTML<\/code> would instead create an HTML injection boundary. Input validation of <code>limit<\/code> cannot make database titles safe for HTML interpretation. The raw title format is deliberate here; HTML entities stored in a title can appear literally and need a separate, tested display policy if decoding is desired.<\/p>\n<h2>5. Separate decisions about caching and authentication<\/h2>\n<p>The example uses <code>no-store<\/code> consistently for successful empty and non-empty responses. That is a cautious starting policy, not a performance optimization. Shared caching is an additional design step: <code>public, max-age=60<\/code> is suitable only for an intentionally shared representation with correct keys and invalidation. Cookies, authorization, language, query parameters and proxy configuration can affect that decision. <code>private<\/code> forbids shared-cache storage but still permits browser storage; <code>no-store<\/code> instructs caches not to store the response. Neither header supplies access control.<\/p>\n<p>For WordPress cookie authentication, a valid <code>wp_rest<\/code> nonce accompanies the login cookie, normally in <code>X-WP-Nonce<\/code>. The protected route must still check the required capability. A nonce is not an API password or a replacement for authorization. External clients can use supported authentication such as application passwords over HTTPS; credentials belong outside browser code.<\/p>\n<h2>6. Correctness and actual cost<\/h2>\n<ul><li>The tests cover the default limit, 1, 100, 0, 101, fractions, scientific notation and arrays. Rejected inputs should produce 400 before the endpoint callback runs.<\/li><li>Further tests cover empty data, titles containing markup, malformed JSON, HTTP errors and client timeout, using the actual subdirectory and permalink configuration.<\/li><li>Access tests cover unpublished, password-protected and membership-restricted content, all supported languages and authenticated versus anonymous behavior.<\/li><li>Headers are checked at the origin and through the real proxy. Measurements cover repeated cold and warm requests, server duration, query counts, memory, time to first byte (TTFB) and typical numbers of simultaneous requests. Comparisons use identical payloads and access policies.<\/li><\/ul>\n<p>A useful performance report states the test environment and the spread of response times. The median is the middle measured value; 95% of the measured response times are at or below the 95th percentile. A single fast response or a smaller JSON payload does not establish a general latency guarantee.<\/p>\n<div class=\"lw-faq\"><h2>Questions and answers<\/h2><h3>Why can a logged-in browser fail a permission check?<\/h3><p>Cookie-based REST authentication also requires the REST nonce. Without the nonce, WordPress treats that cookie-authenticated request as anonymous. Invalid nonces and insufficient capabilities are distinct failures; the final status also depends on the authentication method and permission callback.<\/p><h3>Does REST avoid all plugin work?<\/h3><p>No. Expensive work attached to normal WordPress startup can affect REST too. Profiling identifies the relevant callbacks before a replacement architecture is justified.<\/p><h3>Does the permission callback make public data safe automatically?<\/h3><p>No. Returning true explicitly allows access. The selected fields and records still need a deliberate publication policy, including rules supplied by other plugins.<\/p><\/div>\n<div class=\"lw-quellen\"><h2>Sources<\/h2><ul><li><a href=\"https:\/\/developer.wordpress.org\/rest-api\/\" target=\"_blank\" rel=\"noopener noreferrer\">WordPress REST API<\/a><\/li><li><a href=\"https:\/\/developer.wordpress.org\/rest-api\/extending-the-rest-api\/adding-custom-endpoints\/\" target=\"_blank\" rel=\"noopener noreferrer\">Custom endpoints<\/a><\/li><li><a href=\"https:\/\/developer.wordpress.org\/rest-api\/reference\/posts\/\" target=\"_blank\" rel=\"noopener noreferrer\">Posts endpoint<\/a><\/li><li><a href=\"https:\/\/developer.wordpress.org\/rest-api\/using-the-rest-api\/authentication\/\" target=\"_blank\" rel=\"noopener noreferrer\">Authentication<\/a><\/li><li><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/rest_url\/\" target=\"_blank\" rel=\"noopener noreferrer\">rest_url()<\/a><\/li><li><a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noopener noreferrer\">WP_Query<\/a><\/li><li><a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wpdb\/prepare\/\" target=\"_blank\" rel=\"noopener noreferrer\">wpdb::prepare()<\/a><\/li><li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Node\/textContent\" target=\"_blank\" rel=\"noopener noreferrer\">MDN: textContent<\/a><\/li><li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Reference\/Headers\/Cache-Control\" target=\"_blank\" rel=\"noopener noreferrer\">MDN: Cache-Control<\/a><\/li><\/ul><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A custom WordPress REST endpoint with bounded inputs, explicit access rules and text output. The example uses WP_Query; performance gains require measurements under comparable conditions.<\/p>\n","protected":false},"author":1,"featured_media":33432,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92642],"tags":[91104,91093,91392,91219,91315,91096],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-tutorials-en-wordpress-plugins-tricks","tag-mysql","tag-php","tag-rest-api","tag-tutorial","tag-web-performance","tag-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses | Lukas Wojcik<\/title>\n<meta name=\"description\" content=\"Build custom WordPress REST endpoints: routes, validation, SQL callbacks, fetch requests and permissions with latency checks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses | Lukas Wojcik\" \/>\n<meta property=\"og:description\" content=\"Build custom WordPress REST endpoints: routes, validation, SQL callbacks, fetch requests and permissions with latency checks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/\" \/>\n<meta property=\"og:site_name\" content=\"Lukas Wojcik - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-29T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T11:06:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/11\/flux-voll6-512-512.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Lukas Wojcik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lukas Wojcik\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/\"},\"author\":{\"name\":\"Lukas Wojcik\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"headline\":\"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses\",\"datePublished\":\"2026-09-29T08:00:00+00:00\",\"dateModified\":\"2026-11-15T11:06:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/\"},\"wordCount\":1086,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"image\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/flux-voll6-512-512.png\",\"keywords\":[\"MySQL\",\"PHP\",\"REST API\",\"Tutorial\",\"Web Performance\",\"WordPress\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/\",\"name\":\"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses | Lukas Wojcik\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/flux-voll6-512-512.png\",\"datePublished\":\"2026-09-29T08:00:00+00:00\",\"dateModified\":\"2026-11-15T11:06:18+00:00\",\"description\":\"Build custom WordPress REST endpoints: routes, validation, SQL callbacks, fetch requests and permissions with latency checks.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/flux-voll6-512-512.png\",\"contentUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/flux-voll6-512-512.png\",\"width\":1200,\"height\":630,\"caption\":\"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/en\\\/wordpress-plugins-tricks\\\/tutorials-en-wordpress-plugins-tricks\\\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/de\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/\",\"name\":\"Lukas Wojcik - Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/#\\\/schema\\\/person\\\/895f7604f9b6b71aad9bba33af28d0f9\",\"name\":\"Lukas Wojcik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"url\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\",\"width\":424,\"height\":636,\"caption\":\"Lukas Wojcik\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/lw-x2.jpg\"},\"sameAs\":[\"https:\\\/\\\/www.lukaswojcik.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses | Lukas Wojcik","description":"Build custom WordPress REST endpoints: routes, validation, SQL callbacks, fetch requests and permissions with latency checks.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/","og_locale":"en_US","og_type":"article","og_title":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses | Lukas Wojcik","og_description":"Build custom WordPress REST endpoints: routes, validation, SQL callbacks, fetch requests and permissions with latency checks.","og_url":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/","og_site_name":"Lukas Wojcik - Blog","article_published_time":"2026-09-29T08:00:00+00:00","article_modified_time":"2026-11-15T11:06:18+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/11\/flux-voll6-512-512.png","type":"image\/png"}],"author":"Lukas Wojcik","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Lukas Wojcik","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#article","isPartOf":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/"},"author":{"name":"Lukas Wojcik","@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"headline":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses","datePublished":"2026-09-29T08:00:00+00:00","dateModified":"2026-11-15T11:06:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/"},"wordCount":1086,"commentCount":0,"publisher":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"image":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#primaryimage"},"thumbnailUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/11\/flux-voll6-512-512.png","keywords":["MySQL","PHP","REST API","Tutorial","Web Performance","WordPress"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/","url":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/","name":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses | Lukas Wojcik","isPartOf":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#primaryimage"},"image":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#primaryimage"},"thumbnailUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/11\/flux-voll6-512-512.png","datePublished":"2026-09-29T08:00:00+00:00","dateModified":"2026-11-15T11:06:18+00:00","description":"Build custom WordPress REST endpoints: routes, validation, SQL callbacks, fetch requests and permissions with latency checks.","breadcrumb":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#primaryimage","url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/11\/flux-voll6-512-512.png","contentUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/11\/flux-voll6-512-512.png","width":1200,"height":630,"caption":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses"},{"@type":"BreadcrumbList","@id":"https:\/\/www.lukaswojcik.com\/blog\/en\/wordpress-plugins-tricks\/tutorials-en-wordpress-plugins-tricks\/building-custom-data-apis-in-wordpress-validating-requests-and-limiting-responses\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.lukaswojcik.com\/blog\/de\/"},{"@type":"ListItem","position":2,"name":"Building Custom Data APIs in WordPress: Validating Requests and Limiting Responses"}]},{"@type":"WebSite","@id":"https:\/\/www.lukaswojcik.com\/blog\/#website","url":"https:\/\/www.lukaswojcik.com\/blog\/","name":"Lukas Wojcik - Blog","description":"","publisher":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.lukaswojcik.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.lukaswojcik.com\/blog\/#\/schema\/person\/895f7604f9b6b71aad9bba33af28d0f9","name":"Lukas Wojcik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","url":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","contentUrl":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg","width":424,"height":636,"caption":"Lukas Wojcik"},"logo":{"@id":"https:\/\/www.lukaswojcik.com\/blog\/wp-content\/uploads\/2026\/07\/lw-x2.jpg"},"sameAs":["https:\/\/www.lukaswojcik.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":9,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":26496,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions\/26496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/media\/33432"}],"wp:attachment":[{"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lukaswojcik.com\/blog\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}