網站跳出率一直是廣大站長比較關注的問題,訪客點擊頁面直接跳出一般這個行為我們是無法控制的,只能靠優化用户體驗,提高內容粘性來降低。但是如果訪客點擊頁面上的出站鏈接那麼,這個因素我們是可控制的並且可以強制用户轉跳為站內鏈接,增加網站 PV,並且瞭解用户點擊出站的行為從而更好的來優化網站的用户體驗,那麼 WordPress 網站怎麼來強制用户點擊站外鏈接轉換為站內鏈接呢?代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title> 外鏈跳出統計</title>
<style>
html,body{
    font-size:12px;
    overflow:hidden
}
body{
    padding:0 0 0 0;
    font-size:12px;
    margin:0;
    height:100%;
}
#barframe{
    width:100%;
    height:100%;
    z-index:3;
    position:absolute
}
</style>
</head>
<body>
<?php
if( isset($_GET['go']) || isset($_GET['referer']) ) {
echo "<iframe noresize="noresize" frameborder="0" src=".strip_tags($_GET['go'])." id="barframe"></iframe>";
}else echo '錯誤的參數';
?>
<div style="display:none">
統計代碼
</div>
</body>
</html>

將以上代碼複製後保存為 referer.php 文件,然後放入自己的網站統計代碼上傳至 WordPress 網站的根目錄,然後打開當前使用的 WordPress 主題的 functions.php 文件加入以下代碼:

function wxd_referer(){
echo "<script>
window.onload=function(){
  var a = document.getElementsByTagName('a');
  for(var i=0;i<a.length;i++) a[i].onclick = function(){
    if(this.href.indexOf('javascript:;')==-1 && this.href.indexOf('".$_SERVER['HTTP_HOST']."')==-1)
    this.href='".get_bloginfo('url')."'/referer.php?go=' + encodeURIComponent(this.href) + '&referer=' + window.location.href;
    }
}
</script>";
}
add_action( 'wp_head', 'wxd_referer' );

(PS:該代碼必須加載到 wp_head 鈎子中如果主題沒有加入該鈎子,則代碼不會生效)

效果演示:http://www.baidu.com

html 源代碼中的鏈接依然是百度的但是點擊後就強制轉跳到內鏈了,鏈接參數説明,go 參數表示用户點擊的頁面,referer 參數表示從哪個頁面點擊的出站鏈接,這樣一來在網站數據統計中就可以明確的得知訪客是從哪個鏈接點擊的站外鏈接。