Articles WordPress Development

Building Custom WordPress REST API Endpoints — A Practical Guide

October 25, 2026

Most WordPress developers have at least heard of the REST API. Many have used it to fetch posts from the front end. But the part that actually changes what you can build — writing your own custom endpoints — doesn’t get nearly as much attention. Here’s how to do it properly, with real code.

Why custom endpoints matter

WordPress’s built-in REST routes give you access to posts, pages, users, and taxonomies. That covers a lot of ground. But when you’re building a headless frontend or a third-party integration, you usually need data shaped differently than the defaults provide — or data from multiple sources combined into a single response.

A custom endpoint lets you define exactly what data it returns, how it’s structured, what authentication it requires, and what it accepts as input. You’re not constrained by the built-in routes’ response format or query limitations.

The basic structure: register_rest_route()

Every custom REST endpoint in WordPress is registered with register_rest_route() inside a callback on the rest_api_init action. Here’s a minimal working example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/stats', array(
        'methods'             => 'GET',
        'callback'            => 'my_get_stats',
        'permission_callback' => '__return_true', // public endpoint
    ) );
} );

function my_get_stats( WP_REST_Request $request ): WP_REST_Response {
    $data = array(
        'projects' => wp_count_posts( 'sfp_project' )->publish,
        'articles' => wp_count_posts( 'post' )->publish,
    );
    return new WP_REST_Response( $data, 200 );
}

That registers GET /wp-json/myplugin/v1/stats. Hit it in a browser or with fetch() and you get a JSON response back.

Authentication: when and how

Not every endpoint needs authentication. Public data — blog post listings, project titles, public stats — can use __return_true as the permission callback. For anything sensitive or write-based, you need to verify who’s asking.

  • Application Passwords (built into WordPress 5.6+) — users generate a password in their profile, clients send it as HTTP Basic auth. Best for server-to-server integrations.
  • Nonce authentication — works for logged-in users on the same domain. Useful for admin AJAX replacements.
  • JWT tokens — via a plugin, best for mobile apps and SPAs where you need short-lived stateless tokens.

For most headless builds where the frontend fetches public content at build time, no authentication is needed at all. The build process reads the API, generates HTML, and deploys. The end user never touches the API.

Validating input

If your endpoint accepts parameters, validate everything through the args array in register_rest_route():

'args' => array(
    'per_page' => array(
        'default'           => 10,
        'sanitize_callback' => 'absint',
        'validate_callback' => function ( $val ) {
            return $val > 0 && $val  array(
        'sanitize_callback' => 'sanitize_text_field',
    ),
),

Now $request->get_param('per_page') is guaranteed to be a positive integer under 100. You can trust it without additional checks in your callback.

Returning errors properly

Return WP_Error objects for error conditions. The REST API automatically converts them to properly formatted JSON error responses with the correct HTTP status code:

if ( ! $post_id ) {
    return new WP_Error(
        'not_found',
        'The requested resource was not found.',
        array( 'status' => 404 )
    );
}

Register your routes under a versioned namespace (myplugin/v1) from the start. When you need to change the response shape later, you can add a /v2 namespace without breaking existing clients. It’s the kind of discipline that saves real headaches once an API is in production use.