Archived topic
DNS Prefetch
1 reply · Started by David on May 23, 2021
Where can I edit the code for:
<link rel='dns-prefetch' href='//fonts.googleapis.com' />
<link rel='dns-prefetch' href='//s.w.org' />
I see this in html for every page but I do not know where I can edit the line of code for that section.
Hi David,
Can you specify what you want to modify on these tags?
If you simply want to remove them, you can do it by adding this PHP snippet:
remove_action( 'wp_head', 'wp_resource_hints', 2 );
You can filter out some of the resource hints.
https://developer.wordpress.org/reference/functions/wp_resource_hints/
See the filter in use on the theme code here - https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/general.php#L225
As you can see in the link provided, the theme adds <link rel=’dns-prefetch’ href=’//fonts.googleapis.com’ /> by default.
If you wish to modify the URL, you can do something like this:
Example:
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
foreach ($hints as $key => $hint) {
echo $hint;
if('fonts.googleapis.com' === $hint){
$hints[$key] = 'your URL modified here';
} if('https://s.w.org/images/core/emoji/13.0.0/svg/' === $hint){
$hints[$key] = 'your URL modified here 2';
}
}
}
return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 15, 2 );