Skip to main content
added 322 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

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

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

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 );
added 289 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

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

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.

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 );
added 289 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

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.

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

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

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.

Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102
Loading