2015-02-23 67 views
1

我在我的header.php此代码:如何让jQuery代码不能用于登录Wordpress用户?

<script type="text/javascript" src="/js/jquery.js"></script> 
<script type="text/javascript"> 
jQuery(document).ready(function() { 
    jQuery("a") 
    .filter(function() { 
     var href = jQuery(this).attr('href'); 
     // return true if href exists and matches the regular expression 
     return href && href.match(/mywebsite\.com\/(page)\//); 
    }) 
    .click(function(){ 
     jQuery("a").attr("target","_blank"); 
     url = jQuery(this).attr('href'); 
     jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url)); 
    }); 
}); 
</script> 

我怎样才能使此代码只适用于尚未登录用户的工作? (我正在使用WordPress自我托管)

现在代码打开每个'/ page/*'在一个新的选项卡,并使其通过/te3/out.php,但我希望不会发生登录用户。

我对此很新,所以请让它容易理解。

非常感谢!

+0

http://codex.wordpress.org/Function_Reference/is_user_logged_in – 2015-02-23 13:00:41

+0

当然WordPress的给你,如果你已经登录cookie中如果确实如此,你可以简单地检查此Cookie通过查看字符串:文件.cookie - 如果它在那里然后不加载脚本:http://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js – GavinBrelstaff 2015-02-23 13:18:31

回答

2

只要把条件,如果用户没有登录,然后使用脚本否则不使用。为此,您可以使用is_user_logged_in

<script type="text/javascript" src="/js/jquery.js"></script> 
<?php 
    if (!is_user_logged_in()) : 
?> 
<script type="text/javascript"> 
jQuery(document).ready(function() { 
    jQuery("a") 
     .filter(function() { 
     var href = jQuery(this).attr('href'); 
     // return true if href exists and matches the regular expression 
     return href && href.match(/mywebsite\.com\/(page)\//); 
    }) 
    .click(function(){ 
     jQuery("a").attr("target","_blank"); 
     url = jQuery(this).attr('href'); 
     jQuery(this).attr('href','/te3/out.php?l=click&u=' + escape(url)); 
    }); 
}); 
</script> 
<?php 
    endif; 
?> 
+0

非常感谢你!有用! – Flaviu 2015-02-23 13:29:47