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 Dec 26, 2025
Answer
Creating a custom REST API endpoint in WordPress allows you to extend the REST API with your own functionality. This involves using the `register_rest_route` function to define your endpoint and a callback function to handle requests.
<!-- 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:
- Place the code in your theme's `functions.php` file or a custom plugin.
- Ensure your endpoint is unique by using a namespace like 'myplugin/v1'.
- Use the `WP_REST_Request` object to access request parameters.
- Return a `WP_REST_Response` object to provide a structured response.
Recommended Links:
