今天小編在逛免費資源部落時無意中發現部落的網站頭部加上了一段以前從來沒見過的代碼,下面是當時免費資源部落的頭部代碼截圖:

小編以前也沒見過 dns prefetch 這類標籤,dns prefetch 翻譯成中文大概是 dns 預取、 dns 預讀的意思 (PS:小編英語爛~翻譯的大概意思就是這樣) 。當時小編就覺得很奇怪了這類標籤怎麼沒聽説過啊,正好沒事小編就去請教一下谷歌老師。谷老師立即丟出一條鏈接來:Controlling DNS prefetching 鏈接是 Firefox 官方文檔的,絕對權威。文檔的中説的 dns prefetch 標籤是在讀取一個網站頁面時,把規定好的幾個域名的 dns 事先都解析好,從而達到給網站加速的效果。好了既然瞭解 dns prefetch 標籤的作用,那麼騷年還等什麼動手吧!給自己的網站加上一個。
那麼下面小編來教大家如何讓 WordPress 支持 dns prefetch 標籤。
function wxd_dns_prefetch() {
<meta http-equiv="x-dns-prefetch-control" content="on" />
<link rel="dns-prefetch" href="//bdimg.share.baidu.com/" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" />
<link rel="dns-prefetch" href="//hm.baidu.com/" />
<link rel="dns-prefetch" href="//0.gravatar.com/" />
<link rel="dns-prefetch" href="//1.gravatar.com/" />
}
add_action( 'wp_head', 'wxd_dns_prefetch');
在 WordPress 主題的 functions.php 文件中加上以上代碼後 WordPress 就會自動在頭部載入 dns prefetch 標籤,當然這個前提是你的主題加載了 wp_head() 鈎子。不過以上代碼是全局載入的,也就是説在每個頁面都會載入這段代碼,如果每個頁面都設置了 dns 預取域名的話則每個頁面都要對設置中的域名做一次預取操作這樣一來預取標籤就相當於沒有用了,那麼如何只讓首頁加載 dns 預取標籤呢?其實我們可以給代碼加上個判斷,即可。
function wxd_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/" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" />
<link rel="dns-prefetch" href="//hm.baidu.com/" />
<link rel="dns-prefetch" href="//0.gravatar.com/" />
<link rel="dns-prefetch" href="//1.gravatar.com/" />
<?php }
}
add_action( 'wp_head', 'wxd_dns_prefetch');
但是如果只在網站首頁顯示 dns 預取標籤的話,入口頁面不在首頁的訪客就感覺不到 dns 預取的加速效果了,其實我們可以給訪客的瀏覽器寫入一個 cookies 來判斷訪客是否為第一訪問網站,如果是第一次訪問則在入口頁面輸出 dns 預取標籤,那麼 WordPress 如何來寫入 cookie 呢?小編之前曾經寫過一篇 WordPress 寫入 cookie 的教程 《WordPress 二次開發教程手記:寫入 cookie 記錄訪客行為》,我們再將兩篇文章的代碼結合起來使用,首先在 functions.php 文件中加上以下代碼:
function set_newuser_cookie() {
if (!isset($_COOKIE['wxd_cookie'])) {
setcookie('wxd_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/" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" />
<link rel="dns-prefetch" href="//hm.baidu.com/" />
<link rel="dns-prefetch" href="//0.gravatar.com/" />
<link rel="dns-prefetch" href="//1.gravatar.com/" />
';
}
elseif (isset($_COOKIE['wxd_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/" />
<link rel="dns-prefetch" href="//api.share.baidu.com/" />
<link rel="dns-prefetch" href="//hm.baidu.com/" />
<link rel="dns-prefetch" href="//0.gravatar.com/" />
<link rel="dns-prefetch" href="//1.gravatar.com/" />
';
}
?>
然後在主題的 header.php 文件的</head> 標籤之前加入以下代碼:
<?php include TEMPLATEPATH. '/dns.php'; ?>
好了這樣一來 WordPress 將只在首頁和訪客初次訪問的時候顯示 dns 預取標籤了 (PS:預取域名可以按照自己網站的具體需求去設置) 。另外再説下,大家搞網站都不容易,有時候心血來潮花個幾個小時寫原創也不容易吧?某些人轉載留個鏈接你也不損失什麼是吧?你覺得被人家鄙視好還是留個鏈接好呢?