WordPress.org

Ready to get started?Download WordPress

Forums

How do i know when someone is logged in? (7 posts)

  1. ATENCION
    Member
    Posted 8 years ago #

    Ok..i give up searching for this solution. I need some help from you guys.

    In a page template, I want to be able to show some message if user IS registered and logged in and something different if it´s not.

    I´ve tried the following approach, wich uses a conditional to figure out if the user level is 0. The problem with this is that non-registered users also qualify as true for this code...

    Am i doing something wrong? Is there a better approach, given the fact that I only need to know wether the user is logged in or not?

    Thank you very much!

    <?php
    if ($user_level >= 0) {
    ?>

    "SHOW SOMETHING FOR REGISTERED USERS HERE"

    <?php
    } else {
    ?>

    "SHOW SOMETHING FOR NON-REGISTERED USERS HERE"

    <?php
    }
    ?>

  2. fredrik
    Member
    Posted 8 years ago #

    Maybe you can use the function that is already in WP when for example the admin navigation buttons are showing depending on if you're logged in or not?

  3. ATENCION
    Member
    Posted 8 years ago #

    That´s a good idea, fredrik. Thanks for replying.

    I¨m there, looking for some function wich might be useful but to be honest I´m quite lost and I´m not finding anything that I could tell it´d work.

    Do you have any idea on what exactly to look for? I´m guessing is some sort of conditional, but I don´t really know.

  4. fredrik
    Member
    Posted 8 years ago #

  5. ATENCION
    Member
    Posted 8 years ago #

    Thank you very much!...
    I think I´ve figured it out ...at least it´s working..

    I just check if the $user_ID string has nothing ..

    Here´s how..

    <?php
    if ($user_ID=='') {
    ?>

    "SHOW SOMETHING FOR NON-REGISTERED USERS HERE"

    <?php
    } else {
    ?>
    "SHOW SOMETHING FOR REGISTERED USERS HERE"

    <?php
    }
    ?>

  6. Correct code would be:

    if (!$user_ID) {

  7. VxJasonxV
    Member
    Posted 8 years ago #

    Alternatively,
    if (empty($user_ID)) {

Topic Closed

This topic has been closed to new replies.

About this Topic