Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I create a custom WordPress REST API endpoint?
Asked on Feb 12, 2026
Answer
Creating a custom WordPress REST API endpoint involves using the `register_rest_route` function to define a new route and its callback function. This allows you to extend the REST API with custom functionality.
<!-- BEGIN COPY / PASTE -->
add_action('rest_api_init', function() {
register_rest_route('myplugin/v1', '/custom-endpoint', array(
'methods' => 'GET',
'callback' => 'my_custom_endpoint_callback',
));
});
function my_custom_endpoint_callback($data) {
return new WP_REST_Response('Hello, this is a custom endpoint!', 200);
}
<!-- END COPY / PASTE -->Additional Comment:
- Place the code in your theme's `functions.php` file or in a custom plugin.
- The `register_rest_route` function takes three parameters: namespace, route, and an array of options including methods and callback.
- The callback function should return a `WP_REST_Response` object or an array.
- Ensure your endpoint is unique by using a custom namespace like 'myplugin/v1'.
Recommended Links:
