Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I create a custom REST API endpoint in WordPress?
Asked on Jan 08, 2026
Answer
Creating a custom REST API endpoint in WordPress involves using the `register_rest_route` function to define a new route and its callback function. This is typically done in your theme's `functions.php` file or a custom plugin.
<!-- 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($data) {
return new WP_REST_Response(array('message' => 'Hello, World!'), 200);
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure your callback function returns a `WP_REST_Response` object for proper REST API responses.
- Use the `rest_api_init` action hook to register your custom routes.
- Test your endpoint by visiting `https://yourdomain.com/wp-json/myplugin/v1/data/`.
Recommended Links:
