How to create custom REST API routes in WordPress with register_rest_route

The WordPress register_rest_route function allows developers to register custom REST API routes for WordPress. This function is useful for creating custom endpoints for retrieving, creating, updating, and deleting data from the WordPress database.

Developers can use this function to extend the functionality of the WordPress REST API by defining their own custom endpoints, which can be accessed by external applications or services. This can be particularly useful for creating custom functionality or integrating WordPress with other systems or platforms.

Parameters Accepted by WordPress register_rest_route Function

The register_rest_route function accepts the following parameters:

  • $route_namespace (string, required): The first URL segment after core prefix. It should be unique to your package/plugin.
  • $route (string, required): The base URL for the route you are adding.
  • $args (array, optional, default value: array()): Either an array of options for the endpoint, or an array of arrays for multiple methods.
  • $override (bool, optional, default value: false): If the route already exists, should we override it? True overrides, false merges (with newer overriding if duplicate keys exist).

The function returns a boolean value. It returns true on success and false on error.

Examples

How to register a basic REST route in WordPress

add_action( 'rest_api_init', function () {
 register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
 'methods' => 'GET',
 'callback' => 'my_awesome_author_endpoint',
 ) );
} );

function my_awesome_author_endpoint( $data ) {
 // Handle request and return the author data
}

This code snippet registers a basic REST route in WordPress using the register_rest_route function. It sets up a GET request route for fetching author data based on the author ID.

How to register a REST route with multiple methods in WordPress

add_action( 'rest_api_init', function () {
 register_rest_route( 'myplugin/v1', '/post/(?P<id>\d+)', array(
 array(
 'methods' => 'GET',
 'callback' => 'my_get_post_endpoint',
 ),
 array(
 'methods' => 'POST',
 'callback' => 'my_update_post_endpoint',
 ),
 ) );
} );

function my_get_post_endpoint( $data ) {
 // Handle GET request and return the post data
}

function my_update_post_endpoint( $data ) {
 // Handle POST request and update the post data
}

This code snippet registers a REST route in WordPress using the register_rest_route function with multiple methods (GET and POST). It sets up endpoints for fetching and updating post data based on the post ID.

How to register a REST route with permission callback in WordPress

add_action( 'rest_api_init', function () {
 register_rest_route( 'myplugin/v1', '/private-data/', array(
 'methods' => 'GET',
 'callback' => 'my_private_data_endpoint',
 'permission_callback' => function () {
 return current_user_can( 'read_private_posts' );
 }
 ) );
} );

function my_private_data_endpoint( $data ) {
 // Handle request and return private data
}

This code snippet registers a REST route in WordPress using the register_rest_route function with a permission callback. It sets up a GET request route for fetching private data, and the permission callback function checks if the current user has the capability to read private posts.

Conclusion

In conclusion, the register_rest_route function is a powerful feature for creating custom REST API routes in WordPress. It allows developers to define their own endpoints and specify the callback functions to handle requests. With its flexibility and ease of use, this function opens up a world of possibilities for extending and customizing WordPress websites. By leveraging the register_rest_route function, developers can create seamless integrations with external systems, enhance user experiences, and build robust and scalable applications. Overall, this function is a valuable addition to the WordPress REST API toolkit, and understanding how to use it effectively can greatly enhance a developer’s capabilities.

Related WordPress Functions