Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I create a custom endpoint in the WordPress REST API?
Asked on Feb 24, 2026
Answer
Creating a custom endpoint in the WordPress REST API allows you to extend the API with your own data and functionality. This involves using the `register_rest_route` function within a custom plugin or your theme's `functions.php` file.
<!-- BEGIN COPY / PASTE -->
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', '/myendpoint', array(
'methods' => 'GET',
'callback' => 'my_custom_endpoint_callback',
));
});
function my_custom_endpoint_callback($data) {
return new WP_REST_Response('Hello, World!', 200);
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure your custom endpoint is unique to avoid conflicts with existing routes.
- Use the `rest_api_init` action to register your route when the REST API is initialized.
- Return a `WP_REST_Response` object for proper HTTP status and response formatting.
- Test your endpoint by accessing it via `https://yourwebsite.com/wp-json/myplugin/v1/myendpoint`.
Recommended Links:
