rest_output_link_wp_head() WP 4.4.0
Displays a link to the root REST API route in the <head> section on all site pages.
The link looks like:
<link rel='https://api.w.org/' href='http://wp-test.ru/wp-json/' />
This link is a notification to client applications that the current site has its own API.
By default, this function is called via wp_head hook
add_action( 'wp_head', 'rest_output_link_wp_head', 10, 0 );
A similar link is added to the server response headers by the rest_output_link_header() function that by default is called on the following hook:
add_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
Works based on: get_rest_url()
1 time = 0.004168s = very slow | 50000 times = 4.19s = fast | PHP 7.1.11, WP 4.9.8
No Hooks.
Return
Null. Nothing.
Usage
rest_output_link_wp_head();
Examples
#1 Remove the REST API link
The below code does not disable the REST API but only cancels the link output on all pages of the site. This code can be inserted into a plugin or theme's functions.php file.
remove_action( 'wp_head', 'rest_output_link_wp_head' );
Notes
- See: get_rest_url()
Changelog
Since 4.4.0 | Introduced. |
Code of rest_output_link_wp_head() rest output link wp head WP 5.6
function rest_output_link_wp_head() {
$api_root = get_rest_url();
if ( empty( $api_root ) ) {
return;
}
printf( '<link rel="https://api.w.org/" href="%s" />', esc_url( $api_root ) );
$resource = rest_get_queried_resource_route();
if ( $resource ) {
printf( '<link rel="alternate" type="application/json" href="%s" />', esc_url( rest_url( $resource ) ) );
}
}