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 );
1 time — 0.004168 sec (very slow) | 50000 times — 4.19 sec (fast) | PHP 7.1.11, WP 4.9.8

No Hooks.

Return

null. Nothing (null).

Usage

rest_output_link_wp_head();

Examples

0

#1 Removing the link to the REST API

This code does not disable REST API, but only cancels link output on all pages of the site. The code can be inserted into plugin or functions.php theme directly.

remove_action( 'wp_head', 'rest_output_link_wp_head' );

Notes

Changelog

Since 4.4.0 Introduced.

rest_output_link_wp_head() code WP 6.5.3

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 ) ) );
	}
}