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 Mar 24, 2026
Answer
Creating a custom REST API endpoint in WordPress allows you to extend the functionality of your site by exposing specific data or actions through a custom URL. This is done using WordPress hooks and functions to register the endpoint and define its behavior.
<!-- 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:
- Place the code in your theme's functions.php file or a custom plugin.
- Replace "myplugin/v1" with your desired namespace and version.
- The callback function should return a WP_REST_Response object or a similar structure.
- Test the endpoint by visiting "https://yourdomain.com/wp-json/myplugin/v1/data" in your browser.
Recommended Links:
