網站跳出率一直是廣大站長比較關注的問題,訪客點選頁面直接跳出一般這個行為我們是無法控制的,只能靠最佳化使用者體驗,提高內容粘性來降低。但是如果訪客點選頁面上的出站連結那麼,這個因素我們是可控制的並且可以強制使用者轉跳為站內連結,增加網站 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 引數列示從哪個頁面點選的出站連結,這樣一來在網站資料統計中就可以明確的得知訪客是從哪個連結點選的站外連結。