3

I'm not sure whether my question sounds promising or not but what I'm going to ask is very much relevant. So please read the full question before down voting or flag.

I'm trying to implement DNS Pre-fetching on my wordpress mu, as site by site approach. On my primary site I use a child theme, so I planned to put the following code in the function.php to get what I'm looking for.

function dns_prefetch() {
    echo '<link rel="dns-prefetch" href="//cdn.mysite.com" />';
    echo '<link rel="dns-prefetch" href="//fonts.googleapis.com" />'
}
add_action('wp_head', 'dns_prefetch');

Now the way wordpress functions and hook works is it place this code into the <head> section of the generated html where the wp_head() has been called in the theme's (parent or child) header.php

But as per Google best coding practise, it is best to put DNS Prefetching code as top as possible. As per google it is better to use it like

<html>
<head>
<link rel="dns-prefetch" href="//cdn.mysite.com" />
<link rel="dns-prefetch" href="//fonts.googleapis.com" />
<!-- rest of my the code -->
</head>

But I have no idea how to do it, because all theme put there code first in the header.php before calling wp-head.

So, my question, is there any way to place those dns prefetching code just after starting <head> tag? Please help.

4
  • I really do not see your issue here. The answer from @TheDeadMedic is qiute comprehensive. Copy your parent themes header.php to your child theme and then you can add the link where you want it. Wordpress will load your child header, and not the parent. If you don't want to do this, you will have to do the hack as described in the answer below. Commented Feb 7, 2015 at 18:12
  • But id I do this, then any furthur update on the theme (if anything got changed in the header.php in future update) will not be here on my theme. That is the only reason I'm not overwriting parent themes header.php in the child theme.
    – iSaumya
    Commented Feb 7, 2015 at 18:14
  • You will have to live with that. In my honest opinion, you will still need to update your child theme from time to time, which is recommended Commented Feb 7, 2015 at 18:20
  • But what I'm asking is it safe to use the hacky process mentioned in second? Can it create any security vulnerabilities?
    – iSaumya
    Commented Feb 7, 2015 at 18:22

3 Answers 3

9

As of WordPress 4.6.0 there is a resource hints API that automatically adds all unique enqueued domains, which you can override with wp_resource_hints - you should only use the following answer if you're stuck with < 4.6.0


All you can do is bump the priority of your hook:

add_action( 'wp_head', 'dns_prefetch', 0 /* Highest priority */ );

but does this put my code just after the start of tag?

No, but it will output before any other function attached to wp_head i.e. other <link />'s, most stylesheets, plugin scripts etc.

To put it right after <head> you'll most likely need to edit your theme's header.php directly.

is there really no other way around?

You can hack it with a bit of output buffering:

function wpse_177395_start_buffering( $template ) {
    ob_start();
    return $template;
}

add_filter( 'template_include', 'wpse_177395_start_buffering' );

function wpse_177395_flush() {
    $content = ob_get_clean();
    $content = preg_replace( '/<head[^>]*>/', '$0
    <link rel="dns-prefetch" href="//cdn.mysite.com" />
    <link rel="dns-prefetch" href="//fonts.googleapis.com" />',
        $content
    );

    echo $content;
}

add_action( 'wp_head', 'wpse_177395_flush', 0 );
10
  • but does this put my code just after the start of <head> tag?
    – iSaumya
    Commented Feb 7, 2015 at 17:32
  • 1
    @iSaumya See my revision. Commented Feb 7, 2015 at 17:34
  • 2
    Yes, but it's hacky - see revision. Commented Feb 7, 2015 at 17:46
  • 1
    If you don't want to edit header.php, and you need the code right after the <head>, and there's no other action earlier than wp_head, then it's the only way. Your child theme's functions.php is fine. Commented Feb 7, 2015 at 17:55
  • 2
    It's perfectly safe. The only "hacky" thing about it is the use of output buffering & regex, which is usually considered a last resort (as is the case here). Commented Feb 7, 2015 at 18:27
1

Just small nuggets of information that may help in simple setups (most notably the ones with no use plugins using ob_start):

1) You don't need the to put dns-prefetch calls as the first elements. What you need is to put them as soon as possible before any other element calling external resources.

2) Is actually possible to use negative numbers as priority on add_action.

Therefore, one can use add_action( 'wp_head', 'dns_prefetch', -1 ); and is almost guaranteed to work on said simple setups.

In fact, we've been doing that on https://clinicawp.2aces.com.br jobs and hope it helps you guys.

1
  • 1
    Thanks for your reply. The negative priority is a new thing for me. Haven't seen/used before. Thanks pal.
    – iSaumya
    Commented Aug 28, 2015 at 20:15
0

Child theme's can have their own header.php file, right? So why not just simply drop a child header.php file into the child theme's folder, then add the code there?

Unless I'm missing something, this does seem to be the simplest solution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.