rest_url_prefix
Allows you to change the REST API base (route prefix). For example, from wp-json to api.
Usage
add_filter( 'rest_url_prefix', 'wp_kama_rest_url_prefix_filter' );
/**
* Function for `rest_url_prefix` filter-hook.
*
* @param string $prefix URL prefix.
*
* @return string
*/
function wp_kama_rest_url_prefix_filter( $prefix ){
// filter...
return $prefix;
}
- $prefix(string)
- The current prefix of all REST API routes.
Default: 'wp-json'
Examples
#1 Change the REST API prefix from wp-json to api
## Changes the REST API prefix from `wp-json` to `api`
add_filter( 'rest_url_prefix', 'change_rest_api_prefix' );
function change_rest_api_prefix( $slug ){
return 'api';
}
## When changing the prefix, it is also a good idea to add the new name to the blacklist
add_filter( 'subdirectory_reserved_names', function( $names ){
$names[] = 'api';
return $names;
} );
The rewrite rules must now be refreshed — visit the “Permalinks” admin page.
That is all. Every REST route will now begin with /api/ instead of /wp-json/.
For example, the endpoint (route, URL) for retrieving a post with ID 1 will be:
http://example.com/api/wp/v2/posts/1
Changelog
| Since 4.4.0 | Introduced. |
Where the hook is called
rest_url_prefix
wp-includes/rest-api.php 509
return apply_filters( 'rest_url_prefix', 'wp-json' );