Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I add a custom REST API endpoint in WordPress? Pending Review
Asked on Apr 11, 2026
Answer
To add a custom REST API endpoint in WordPress, you need to use the `register_rest_route` function within a custom plugin or your theme's `functions.php` file. This function allows you to define a new route and specify the callback function that handles requests to that route.
<!-- 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('Hello, World!', 200);
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have a clear namespace (e.g., "myplugin/v1") to avoid conflicts with other plugins or themes.
- Use appropriate HTTP methods (GET, POST, etc.) based on the action your endpoint performs.
- Test your endpoint by visiting "https://yourwebsite.com/wp-json/myplugin/v1/custom-endpoint" in your browser or using a tool like Postman.
Recommended Links:
