if user not logged in

sahanz
sahanz used Ask the Experts™
on
I want to add a javascript to the page if user is not logged, how can I do that ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Echo the Javascript into the browser output stream after testing with PHP on the server side to see if the client is logged in.
Most Valuable Expert 2011
Top Expert 2016

Commented:
To try to explain a little more, there is a PHP variable set when the client is logged in - this is probably set in the $_SESSION array (not exactly sure how WP does this).  Test this variable and if it shows the client is not logged in, then drop out of PHP into HTML and squirt the JavaScript code right into the page.  You would probably do this in two steps - one to add the <script> tag into the header of the page and another to call the JavaScript during the page load.

HTH, ~Ray
Use this to add a script if the user is not logged on wordpress
<?php
if(isset($_COOKIE['USER_COOKIE'])){
  echo '<script language="javascript" type="text/javascript" src="/script_path/yourscript.js">';
}
?>

Open in new window

Try this:

<?php
if(!isset($_SESSION['logged_in_user']) || empty($_SESSION['logged_in_user']))
?>
<script language="javascript">
// Required functions/statements goes here
</script>
<?php
}
?>

thanks
Commented:
<?php
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
};
?>

I found this, but first 3 answers make sense to me. Thanks for replies.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial