1

I have this code in my 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>

How can I make this code only work for users that are not logged in? (I'm using wordpress self-hosted)

Right now the code opens every '/page/*' in a new tab and makes it go through /te3/out.php but I would like that not to happen to logged in users.

I'm pretty new to this so please make it easy to understand.

Thanks a lot!

Flaviu
  • 47
  • 8
  • http://codex.wordpress.org/Function_Reference/is_user_logged_in – Michael Laffargue Feb 23 '15 at 13:00
  • Surely wordpress gives you a cookie if you are logged in. If it does you can simply check if that cookie is present by looking at string: document.cookie - if it is there then don't load the script: http://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js – GavinBrelstaff Feb 23 '15 at 13:18

1 Answers1

2

Simply put condition that if user is not logged in then use script else not.For that you can use 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;
?>
Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32