WordPress.org

Forums

conditional if user logged in display logout link (7 posts)

  1. bucchow
    Member
    Posted 3 years ago #

    I need to display a logout link only if the user is logged in. If the user is NOT logged in it should display nothing. Redirect url upon logout may be helpful as well. Help is greatly appreciated!

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Function_Reference/wp_logout_url

    should be what you are looking for?

    Not sure if it displays anything to users not logged in.... if it does, you could wrap it in a is_user_logged_in

    http://codex.wordpress.org/Function_Reference/is_user_logged_in

  3. bucchow
    Member
    Posted 3 years ago #

    Thanks so much. Dealing with a noob here. How do I correctly wrap one around the other? wp_logout_url is present whether logged in or not so I would have to use the is_user_logged_in

    Thanks in advance!

  4. bucchow
    Member
    Posted 3 years ago #

    Would anyone be able to show me how to wrap a Function Reference/is user logged in around a Function Reference/wp logout url? I'd like to display a logout link only if the user is logged in. If the user is NOT logged in it should display nothing.

    Thank you!!

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    <?php
    if ( is_user_logged_in() ) {
        echo wp_logout_url( home_url() );
    }
    ?>

    as a guess....

  6. bucchow
    Member
    Posted 3 years ago #

    That got me on the right track. Here's what finally worked for me. It lands on the login/logout wp page but that's alright by me.

    <?php
    if ( is_user_logged_in() ) {
        echo '<a href="'. wp_logout_url() .'">Logout</a>';
    }
    ?>

    Thank you so much for your help!

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    You can modify where it lands if you like.... by changing the redirect within wp_logout_url. That's all documented on the codex page... glad you got it working!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.