wordpress 手机端自动跳转二级域名方法

wordpress 手机端自动跳转二级域名,把以下代码放在head中间:

<script type="text/javascript">
(function(){
   var user = navigator.userAgent;
var Agents = ["Android","Linux","iPhone"];
var flag = false;
for (var i = 0;i<Agents.length;i++) {
       if (user.indexOf(Agents[i]) > 0){
        flag = true;
       }
   }
   if(flag == true){
       location = "http://m.域名";
   }
})();
</script>