如何使jQuery代码不适用于已登录的Wordpress用户

How to make jQuery code not work for logged in Wordpress users?

本文关键字:登录 Wordpress 用户 适用于 何使 jQuery 代码 不适用      更新时间:2024-04-10

我的header.php中有以下代码:

<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,但我希望登录的用户不要发生这种情况。

我对此还很陌生,所以请让它易于理解。

非常感谢!

只需设置条件,即如果用户未登录,则不使用脚本else。为此,您可以使用is_user_loged_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;
?>