Skip to content
Deluxe Blog Tips
WordPress Tutorials, Tips and Tricks
  • Browse
    • WordPress
    • JavaScript
    • Tools
    • Server
    • PHP
    • SEO
  • Plugins
    • Meta Box
    • Slim SEO
    • WP Auto Listings
    • View All
  • Themes
WordPress: Check if user is logged in JavaScript
  • JavaScript
  • WordPress

WordPress: How to Check If User is Logged in with JavaScript

Anh TranMarch 14, 2019March 12, 2019

It’s easy to check if the current user is logged in with PHP in WordPress. Simply use is_user_logged_in() function. However, how to do it with JavaScript?

WordPress uses cookies to track logged in users. However, handling cookies is quite complicated, since WordPress doesn’t use a consistent cookie name for this purpose.

However, there’s a better and a simpler way to check the user status. If your theme has body_class() in the <body> tag (actually, all good WordPress themes have to implement this), then you’ll see logged-in users have logged-in class in the body tag.

Using this understanding, we can check if the current user is logged in with JavaScript simply, like this:

document.body.classList.contains( 'logged-in' );

That’s all!

It’s important to note that, your theme must have body_class() in the <body> tag. If you don’t see that, you should add this function yourself, or switch to another theme.

javascriptloginuser

Post navigation

Gutenberg JS Config
Previous Post
Minimal JS Config for Gutenberg Build Process (no Webpack)
Mobirise review - a free website builder
Next Post
Mobirise: Must-have Free Website Builder in 2019
wordpress custom fields plugin
Check out my plugin, Meta Box, to instantly generate expert custom meta boxes and custom fields for WordPress.
wordpress business theme
My best selling theme, TheFour, helps you build your business websites in no time at all.
Slim SEO WordPress Plugin
A fast, lightweight WordPress SEO plugin. Slim SEO automates all the SEO work for your websites in no time at all.

free WordPress theme reviews

Freewpthemes.reviews provides you with the most in-depth and honest reviews of 101+ WordPress themes.

Copyright © 2021 Deluxe Blog Tips. Theme eStar by GretaThemes.
Meta Box - The most lightweight yet powerful WordPress custom fields plugin. Get It Now!