Which setting define injects inside WordPress head?

I have seen it is added injects like Meta data:

<link rel="dns-prefetch" href="//cdnjs.cloudflare.com" />
<link rel="dns-prefetch" href="//maps.googleapis.com" /> 
<link rel="dns-prefetch" href="//fonts.googleapis.com" />

How to manage and eliminate those inserts inside <?php wp_head(); ?>

Hi,

To remove all dns-prefetch links from the wp_head() action, you can add the following to functions.php:

add_action( 'init', 'remove_dns_prefetch' );
function remove_dns_prefetch() {
  remove_action( 'wp_head', 'wp_resource_hints', 2 );
}

The init hook is one of WordPress’s action hooks that runs after WordPress has finished loading but before any headers are sent. It is often used to initialize settings, functions, and resources needed by a theme or plugin.

To remove only certain links:

function remove_dns_prefetch($hints, $relation_type) {
  if ('dns-prefetch' === $relation_type) {
    return array_diff($hints, array(
      '//cdnjs.cloudflare.com',
      '//maps.googleapis.com',
      '//fonts.googleapis.com',
    ));
  }
  return $hints;
}
add_filter('wp_resource_hints', 'remove_dns_prefetch', 10, 2);

This uses a filter, which takes an input, modifies it, and returns it.

However, it’s worth noting that dns-prefetch is used to improve website performance, so removing it could lead to slower load times for external resources. Is there any particular reason you want to remove these links?

Cleaning up WordPress head code is a good idea for a variety of reasons. Here are a couple of resources you might be interested in:

1 Like

How to eliminate secured data which are public? An example:

<link rel="https://api.w.org/" href="https://example.com/wp-json/" />
<link rel="alternate" type="application/json" href="https://example.com/wp-json/wp/v2/pages/XXX" />
<link rel="canonical" href="https://example.com/XXX/" /> <link rel="shortlink" href="https://example.com/?p=XXX" />

Add the following to your theme’s functions.php file:

remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'rel_canonical');

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.