The rest_endpoints WordPress PHP filter allows you to modify the available REST API endpoints.
Usage
add_filter('rest_endpoints', 'your_custom_function'); function your_custom_function($endpoints) { // your custom code here return $endpoints; }
Parameters
- $endpoints (array): The available endpoints. An array of matching regex patterns, each mapped to an array of callbacks for the endpoint. These take the format
'/path/regex' => array( $callback, $bitmask )
or'/path/regex' => array( array( $callback, $bitmask )
.
More information
See WordPress Developer Resources: rest_endpoints
Examples
Disable /users rest routes
add_filter('rest_endpoints', function( $endpoints ) { if ( isset( $endpoints['/wp/v2/users'] ) ) { unset( $endpoints['/wp/v2/users'] ); } if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) { unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ); } return $endpoints; });
Remove a specific endpoint
Remove the /wp/v2/posts
endpoint from the available endpoints.
add_filter('rest_endpoints', 'remove_specific_endpoint'); function remove_specific_endpoint($endpoints) { unset($endpoints['/wp/v2/posts']); return $endpoints; }
Add a custom endpoint
Add a custom endpoint /my-plugin/v1/data
with a custom callback function.
add_filter('rest_endpoints', 'add_custom_endpoint'); function add_custom_endpoint($endpoints) { $endpoints['/my-plugin/v1/data'] = array('my_custom_callback', 0); return $endpoints; }
Modify an existing endpoint’s bitmask
Change the bitmask of an existing endpoint /wp/v2/users
to require authentication.
add_filter('rest_endpoints', 'modify_existing_endpoint'); function modify_existing_endpoint($endpoints) { $endpoints['/wp/v2/users'][1] = 1; return $endpoints; }
Add multiple custom endpoints
Add multiple custom endpoints with different regex patterns and callbacks.
add_filter('rest_endpoints', 'add_multiple_custom_endpoints'); function add_multiple_custom_endpoints($endpoints) { $endpoints['/my-plugin/v1/data'] = array('my_custom_callback1', 0); $endpoints['/my-plugin/v1/settings'] = array('my_custom_callback2', 0); return $endpoints; }
Remove all endpoints
Remove all available REST API endpoints.
add_filter('rest_endpoints', 'remove_all_endpoints'); function remove_all_endpoints($endpoints) { return array(); }