当前位置:
首页 > WordPress教程 > wordpress加速优化进阶教程:DNS 预获取(DNS Prefetching)

wordpress加速优化进阶教程:DNS 预获取(DNS Prefetching)

今天小V在逛免费资源部落时无意中发现部落的网站头部加上了一段以前从来没见过的代码,下面是当时免费资源部落的头部代码截图:

DNS prefetching(dns预读)

小V以前也没见过dns prefetch这类标签,dns prefetch翻译成中文大概是dns预取、dns预读的意思(PS:小V英语烂~翻译的大概意思就是这样)。当时小V就觉得很奇怪了这类标签怎么没听说过啊,正好没事小V就去请教一下谷歌老师。谷老师立即丢出一条链接来:Controlling DNS prefetching 链接是Firefox官方文档的,绝对权威。文档的中说的dns prefetch标签是在读取一个网站页面时,把规定好的几个域名的dns事先都解析好,从而达到给网站加速的效果。好了既然了解dns prefetch标签的作用,那么骚年还等什么动手吧!给自己的网站加上一个。

那么下面小V来教大家如何让wordpress支持dns prefetch标签。

function v7v3_dns_prefetch() {
<meta http-equiv="x-dns-prefetch-control" content="on" />
<link rel="dns-prefetch" href="//bdimg.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//hm.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//0.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//1.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
}
add_action( 'wp_head', 'v7v3_dns_prefetch');

在wordpress主题的functions.php文件中加上以上代码后wordpress就会自动在头部载入dns prefetch标签,当然这个前提是你的主题加载了wp_head()钩子。不过以上代码是全局载入的,也就是说在每个页面都会载入这段代码,如果每个页面都设置了dns预取域名的话则每个页面都要对设置中的域名做一次预取操作这样一来预取标签就相当于没有用了,那么如何只让首页加载dns预取标签呢?其实我们可以给代码加上个判断,即可。

function v7v3_dns_prefetch() {
wp_reset_query(); if(is_home()){?>
<meta http-equiv="x-dns-prefetch-control" content="on" />
<link rel="dns-prefetch" href="//bdimg.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//hm.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//0.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//1.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<?php }
}
add_action( 'wp_head', 'v7v3_dns_prefetch');

但是如果只在网站首页显示dns预取标签的话,入口页面不在首页的访客就感觉不到dns预取的加速效果了,其实我们可以给访客的浏览器写入一个cookies来判断访客是否为第一访问网站,如果是第一次访问则在入口页面输出dns预取标签,那么wordpress如何来写入cookie呢?小V之前曾经写过一篇wordpress写入cookie的教程《wordpress二次开发教程手记:写入cookie记录访客行为》,我们再将两篇文章的代码结合起来使用,首先在functions.php文件中加上以下代码:

function set_newuser_cookie() {
if (!isset($_COOKIE['v7v3_cookie'])) {
setcookie('v7v3_cookie', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
add_action('after_setup_theme', 'set_newuser_cookie');

然后在主题文件夹下新建一个dns.php文件并加入以下代码:

<?php
if (is_home()) { //判断当前页面是否为首页
echo '
<meta http-equiv="x-dns-prefetch-control" content="on" /> //开启dns预取
<link rel="dns-prefetch" href="//bdimg.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//hm.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//0.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//1.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
';
}

elseif (isset($_COOKIE['v7v3_cookie'])) { //判断是否为初次访问
echo '<meta name="author" content="WordPress Dns Prefetch Is By NaiZui" />
';
}
else {
echo '
<meta http-equiv="x-dns-prefetch-control" content="on" />
<link rel="dns-prefetch" href="//bdimg.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//hm.baidu.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//0.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<link rel="dns-prefetch" href="//1.gravatar.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
';
}
?>

然后在主题的header.php文件的</head>标签之前加入以下代码:

<?php include TEMPLATEPATH. '/dns.php'; ?>

好了这样一来wordpress将只在首页和访客初次访问的时候显示dns预取标签了(PS:预取域名可以按照自己网站的具体需求去设置)。

原文地址:http://v7v3.com/wpjiaocheng/201308250.html

wordpress加速优化进阶教程:DNS 预获取(DNS Prefetching):等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!