Optimize Links In Header

 

WordPress uses some types of REST API functionality. This is noticeable in the links in the WordPress header. This is cool if your site uses it. But if not, then you can remove all the extra baggage that WordPress now adds to each page load. This article explains how to disable it.

Note: Before using any of these methods, make sure that your site does not need any REST API features. That is, turn off only if you know what you’re doing!)

Why?

Why would anyone do that? Because not everyone uses or wants to use the WP REST API, link headers and associated markup are not always required. For example, none of my sites use anything with the REST API. I like to make things easy, fast and relevant. One way to do this is to turn off everything unnecessary.

This article explains how to disable the three bits of code that WordPress now adds to interface pages:

  • REST API normative tag
  • oEmbed Discovery Links
  • REST API links in HTTP headers

So now let’s look at each of these three items and see how to disable them.

 

 

Disable REST API link tag

Link tag to all front-end pages:

<link rel=’https://api.w.org/’ href=’http://example.com/path/?rest_route=/’ />

Solve: add the following line to – functions.php:

remove_action(‘wp_head’, ‘rest_output_link_wp_head’, 10);

 

 

Disable oEmbed Discovery Links

oEmbed discovery links to all single-view pages:

<link rel=“alternate” type=“application/json+oembed” href=“http://example.com/path/?rest_route=%2Foembed%2F1.0%2Fembed&#038;url=http%3A%2F%2Fexample.com%2Fpath%2F%3Fp%3D1” /> <link rel=“alternate”

type=“text/xml+oembed” href=“http://example.com/path/?rest_route=%2Foembed%2F1.0%2Fembed&#038;url=http%3A%2F%2Fexample.com%2Fpath%2F%3Fp%3D1&#038;format=xml” />

Solve: Disable oEmbed by adding the following line to – functions.php:

remove_action(‘wp_head’, ‘wp_oembed_add_discovery_links’, 10);

 

 

Disable REST API link in HTTP

All page requests:

Link: <http://example.com/path/wp-json/>; rel=”https://api.w.org/”

Solve: REST Api link you can replaced in – functions.php:

remove_action(‘template_redirect’, ‘rest_output_link_header’, 11, 0);

 

 

All together

Putting these three techniques together, we get the following chunk of code:

// Disable REST API link tag remove_action(‘wp_head’, ‘rest_output_link_wp_head’, 10);

// Disable oEmbed Discovery Links remove_action(‘wp_head’, ‘wp_oembed_add_discovery_links’, 10);

// Disable REST API link in HTTP headers remove_action(‘template_redirect’, ‘rest_output_link_header’, 11, 0);

 

 

2 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.