WordPress Q&As Logo
WordPress Q&As Part of the Q&A Network
Q&A Logo

How can I create a custom REST API endpoint in WordPress?

Asked on Aug 27, 2025

Answer

Creating a custom REST API endpoint in WordPress involves using the `register_rest_route` function to define a new route and specify a callback function to handle the request. This allows you to extend WordPress's REST API with your own custom functionality.
<!-- BEGIN COPY / PASTE -->
    add_action('rest_api_init', function () {
        register_rest_route('myplugin/v1', '/data/', array(
            'methods' => 'GET',
            'callback' => 'my_custom_endpoint_callback',
        ));
    });

    function my_custom_endpoint_callback(WP_REST_Request $request) {
        return new WP_REST_Response(array('message' => 'Hello, World!'), 200);
    }
    <!-- END COPY / PASTE -->
Additional Comment:
  • Use the `rest_api_init` action to register your custom route.
  • The `register_rest_route` function requires a namespace, route, and an array of options including the HTTP method and callback.
  • The callback function should return a `WP_REST_Response` object for proper REST API responses.
  • Ensure your endpoint is secure by implementing proper authentication and permission checks if needed.
✅ Answered with WordPress best practices.

← Back to All Questions
The Q&A Network