What is the register_rest_route()
function used for?
The register_rest_route()
function is used to create custom REST API routes in WordPress. REST APIs (Representational State Transfer Application Programming Interfaces) provide a standardized way for web developers to communicate with web servers. WordPress offers a built-in REST API that allows developers to create custom endpoints that can be used to retrieve or modify site data. The register_rest_route()
function allows developers to create their own custom endpoints and specify the callback functions that will be called when those endpoints are requested.
Description of register_rest_route()
Parameters
The register_rest_route()
function has four required parameters and two optional parameters:
$namespace
(required) – The namespace for the route. This is the prefix that will be used in the URL for the custom endpoint. For example, if the namespace is “myplugin/v1”, the endpoint URL would be “https://example.com/wp-json/myplugin/v1/my_endpoint“.$route
(required) – The route for the endpoint. This is the suffix that will be used in the URL for the custom endpoint. For example, if the route is “my_endpoint”, the endpoint URL would be “https://example.com/wp-json/myplugin/v1/my_endpoint“.$args
(required) – An array of arguments that define the behavior of the endpoint. This includes things like the HTTP method (GET, POST, PUT, DELETE), the callback function, and any permissions required to access the endpoint.$override
(required) – A boolean value that indicates whether the endpoint should override any existing endpoints with the same namespace and route. If set to, the new endpoint will replace the existing one. If set tofalse
, an error will be thrown if an existing endpoint with the same namespace and route is found.$methods
(optional) – An array of HTTP methods that are allowed for the endpoint. If not specified, all methods are allowed.$permission_callback
(optional) – A callback function that determines whether the user has permission to access the endpoint. If not specified, the endpoint will be publicly accessible.
How to use register_rest_route()
in WordPress
Example. Here we first register our custom rest routes on rest_api_init action with our custom function myplugin_register_routes. Function registers one custom rest route on ‘myplugin/v1/posts/(?P<id>\d+)’ Rest API URL.
As we can see, the register_rest_route() function declares that a myplugin_get_post function will run whenever /myplugin/v1/posts/POST_ID URL will be hit.
The function will return an array containing the post ID, title, and content.
<?php
// Register rest routes
add_action( 'rest_api_init', 'myplugin_register_routes' );
function myplugin_register_routes() {
register_rest_route('myplugin/v1', '/posts/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => 'myplugin_get_post',
'permission_callback' => '__return_true' // Allow any user to access
]);
}
function myplugin_get_post($request) {
$id = $request->get_param('id');
$post = get_post($id);
return [
'id' => $post->ID,
'title' => $post->post_title,
'content' => $post->post_content,
];
}
Conclusion
As you can see, register_rest_route() is used when you need to create a custom rest API route. Feel free to try it out in your own custom development.