Is there a way to call via javascript if a user is logged-in on a static html file?

I am using Wordpress/Woocommerce for my website along with jekyll (static html generator). I want to change a button based upon user logged in or logged out.

\\ if user is logged in
buttonLog Out/button
\\ else
buttonLog in/button

Topic rest-api static-website api users login Wordpress

Category Web


Not sure about jekyll, but it is totally possible (though not recommended) in normal setup if the theme you are using calling the classes in body element using body_class() (almost all themes do). WordPress adds a classname logged-in in body if the user is logged in. Check for that and you are good to go.

Here is the jquery code to help you get started.

if ( $('body').hasClass('logged-in') ) {
    //Do your magic for logged in users
} else {
    //Do your magic for non logged in users
}  

This can be done using native JavaScript too, just google for a solution to check for classes.


You can do this.

<?php if (is_user_logged_in()) : // user is logged in ?>
<button>Log Out</button>
<?php else : // user is not logged in ?>
<button>Log in</button>
<?php endif; ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.