Question: there is a way to check wordpress cookie **wordpress\_logged\_in** using jquery?

I'm using cloudflare full caching, so there is no way to check if user is logged or not than this cookie, I tried to read it using jquery but seems protected and not visible, I can access it only using PHP.

Here is how I can read it right now :

```
$logged_in = 'no'; 
if (count($_COOKIE)) {
    foreach ($_COOKIE as $key => $val) {
        if (preg_match("/wordpress_logged_in/i", $key)) {
            $logged_in = 'yes';
        }       
    }
}

```

Maybe there is a possiblity to check it simply by jquery, but I didn't find how.

Thanks for helping

Answer: 
If you only want to know if a user is logged in you can check if the body element has class 'logged-in' i.e.

```
jQuery( 'body' ).hasClass( 'logged-in' )

```

WordPress adds this class to the body element if a user is logged in.
As mentioned in another answer, `wp_localize_script()` is a useful function. I use this, specifically:

```
wp_localize_script(
    'site-js',
    'siteVars',
    array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'isMobile' => (wp_is_mobile() ? true : false),
        'isHome' => (is_home() || is_front_page() ? true : false),
        'isSingle' => (is_single() ? true : false),
        'isLogged' => (is_user_logged_in() ? true : false),
    )
);

```

Then you can access it in your site script like `siteVars.isLogged` which will return the correct boolean value. Place this in your `functions.php` file. I prefer to place this in a function and execute it right after you enqueue your scripts.

You *might* be able to use a script such as [js-cookie](https://github.com/js-cookie/js-cookie) -- I've used that script to set/read cookie data through WP several times. Though the first method with `wp_localize_script()` is probably more reliable. If the cookie data is protected then the JS solution might not work. The PHP solution is a for-sure thing. You could even extend it to get the users role and user information. 

<https://developer.wordpress.org/reference/functions/is_user_logged_in/>

<https://codex.wordpress.org/Function_Reference/wp_localize_script>