The create_initial_rest_routes() WordPress PHP function is used to register the default REST API routes.
Usage
create_initial_rest_routes();
Since this function doesn’t accept any parameters, it’s as simple as calling the function in your code. Once called, the default REST API routes will be registered within your WordPress application.
Parameters
- This function does not accept any parameters.
More information
See WordPress Developer Resources: create_initial_rest_routes()
Please note that this function is implemented in WordPress version 4.4.0. The source code can be found in wp-includes/rest-api.php
.
Examples
Registering Default REST API Routes
This example registers the default REST API routes.
// Call the function create_initial_rest_routes(); // Now your WordPress application has the default REST API routes registered.
Checking if Default Routes are Registered
In this example, we first check if the REST API routes are available before attempting to register them.
// If the REST API routes are not available if (!class_exists('WP_REST_Controller')) { // Register the default routes create_initial_rest_routes(); }
Re-registering REST API Routes after Unregistration
Suppose you’ve unregistered some default REST API routes for some reason. You can re-register them using create_initial_rest_routes()
// Unregister a route unregister_rest_route('wp/v2', '/posts'); // Register all default routes again create_initial_rest_routes();
Adding Custom Functionality after Registering Routes
Here’s an example where you might add your own functionality after the default routes have been registered.
// Register the default routes create_initial_rest_routes(); // Add your custom functionality add_action('rest_api_init', 'my_custom_function');
Registering Default Routes on Plugin Activation
This example shows you how to register the default REST API routes when a plugin is activated.
register_activation_hook(__FILE__, 'my_plugin_activation'); function my_plugin_activation() { // Register the default routes create_initial_rest_routes(); }