4

I use FaceBook JS SDK to check whether the user has permission to log in to my site or not. If user has permission I log him into the WP site.

It is possible to check with JS whether the user already logged in or not? Possible to check existance of wordpress_logged_in_ cookie, and how?

I am trying with this code, but doesn't work:

if (document.cookie.search(/^wordpress_logged_in_\w*/) >= 0) {
  // Cookei exsist.
  alert("Cookie");
}

thanks,

2 Answers 2

3

Just use wp_localize_script() and is_user_logged_in().

wp_localize_script( 
    'my-ajax-request', 
    'MyAjax', 
    array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'logged_in' => is_user_logged_in()
    ) 
);
0
<script type="text/javascript">
  logged = <?php if(is_user_logged_in()): echo 1 ; else: echo 0 ; endif; ?> ;
  if(!logged){
    // Execute Google Analytics Code Here
  }
</script>

A bit of PHP can make it a whole lot easier. It will work in wordpress 2.0.0+

1
  • Still works now, nice solution! Commented Mar 3, 2019 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.