Checking if user is logged in a post or page or portfolio (2 posts)

  1. biagioblack
    Posted 3 years ago #

    I have a post that shows up on my homepage board (see http://www.grossify.com) that contains a registration form. I would like to detect if the user is logged in. If not, it should show the form. If logged in, it should hide it.

    I created a .js file that I've put in the wp-content directory. I then reference it in the post with the following code:

    <script type="text/javascript" src="/wp-content/custom-scripts/grossify.js"></script>
    <script type="text/javascript">

    The problem is I don't know how to determine in the JS function showRegistrationForm() in the .js file if the user is logged in.


    I know there are PHP functions wp_localize_script() and is_user_logged_in(). However, this does no good in .js. I don't know enough about JS and PHP to figure this out.

  2. mrpritchett
    Posted 3 years ago #

    Your best bet is use PHP and use is_user_logged_in(). Something like this:

    `<?php if ( !is_user_logged_in() ) { ?>
    "Your javascript includes here"
    <?php { ?>

    The "!" indicates that it is the opposite of the condition so "use is NOT logged in."

Topic Closed

This topic has been closed to new replies.

About this Topic