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 Mar 21, 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 registering a new route and defining a callback function to handle requests.
<!-- 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(WP_REST_Request $request) {
return new WP_REST_Response(array('message' => 'Hello, World!'), 200);
}
<!-- END COPY / PASTE -->Additional Comment:
- Place this code in your theme's functions.php file or a custom plugin.
- The "myplugin/v1" is the namespace and version for your custom endpoint.
- The callback function should handle the request and return a WP_REST_Response object.
- Ensure you have the REST API enabled and accessible on your WordPress site.
Recommended Links:
