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 Feb 01, 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 allows you to extend the WordPress REST API with your own custom functionality.
<!-- 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.
- Replace `'myplugin/v1'` and `/data/` with your desired namespace and endpoint path.
- The callback function should handle the request and return a `WP_REST_Response` object.
- Test the endpoint by visiting `https://yourwebsite.com/wp-json/myplugin/v1/data/`.
Recommended Links:
