网站跳出率一直是广大站长比较关注的问题,访客点击页面直接跳出一般这个行为我们是无法控制的,只能靠优化用户体验,提高内容粘性来降低。但是如果访客点击页面上的出站链接那么,这个因素我们是可控制的并且可以强制用户转跳为站内链接,增加网站 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 参数表示从哪个页面点击的出站链接,这样一来在网站数据统计中就可以明确的得知访客是从哪个链接点击的站外链接。