title: Don't run code for Logged in users
body: <p>I'm trying to stop this script from running if the visitor is logged in.</p>
  
  <p>What the script does is add defer and async attribute to JavaScript files. But it breaks site if done to logged in visitors.</p>
  
  <p>Following is the code that is in the functions.php file:</p>
  
  <pre><code>function js_async_attr($tag)
  {
      if ( !is_user_logged_in() )
      {
          $scripts_to_exclude = array('script1.js', 'script2.js', 'script3.js');
          foreach($scripts_to_exclude as $exclude_script)
          {
              if(true == strpos($tag, $exclude_script ))
              return $tag; 
          }
          return str_replace( ' src', ' defer async src', $tag );
      }
  }
  add_filter( 'script_loader_tag', 'js_async_attr', 10 );
  </code></pre>
  
  <p>But it is not working properly, It is supposed to run only for logged out visitors. But it is not running for logged out visitors and break the pages for logged in visitors.</p>