WordPress : is_user_logged_in

Discussion in 'Templates customization' started by isabel, Dec 27, 2011.

  1. isabel

    isabel New Member

    Joined:
    Dec 20, 2011
    Messages:
    5
    I'd like to build some conditionals in to my WordPress theme.
    A simple check I've used before while displaying content to a logged in user is:
    is_user_logged_in

    if ( is_user_logged_in() ) {
    echo 'Member Dashboard Link';
    } else {
    // shhh, say nothing!
    }

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

    When I login as a user, it doesn't seem to recognize me as logged in!

    I know the widget seems to work, but I want more control of the members experience ...
    and do some cool theme tricks based on whether the member is logged in/out.

    Anyone have this working?
  2. isabel

    isabel New Member

    Joined:
    Dec 20, 2011
    Messages:
    5
  3. davidm1

    davidm1 aMember User & Partner

    Joined:
    May 16, 2006
    Messages:
    4,437
    Are these tricks done within the wordpress post/page, or in the theme itself?

    David
  4. isabel

    isabel New Member

    Joined:
    Dec 20, 2011
    Messages:
    5
    @davidm1, thanks for you reply.
    Not with in the post / page content, but with in the theme.

    Say for example:
    I have two 'menus' to show in the header.
    1. Member Center Dashboard ( user logged in )
    2. Sign up | Login ( user logged out )

    or on the home page:
    Code:
    <?php if ( is_user_logged_in() ) : //  Member logged in ?>
    
            Member Home: Resources
    
            <?php else: // Member not logged in ?>
    
            Guest Home : Sign up
    
            <?php endif  ?>
      
    I understand I can do some of this with [shortcodes] - I want to be able to do it on the wordpress theme level.
  5. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    amember plugin widgets and protection system do not require user to be logged in wordpress.
    It use aMember sessions instead.
    If after login is_user_logged_in() function does not work right - single login does not work between aMember and wordpress. Check all security key values in aMember CP -> Setup -> Wordpress and make sure they are the same as in wordpress config file.
    Also make sure that both wordpress and aMember use the same domain name in urls.
    For example if user login into aMember at www.domain.com and then redirected to wordpress at domain.com single login will not work in most cases because domains are different.

    In any way you can use amember's code in order to check is user logged in amember or not:
    PHP:
    <?php if(am4PluginsManager::getAPI()->isLoggedIn())   : //  Member logged in ?>

            Member Home: Resources

            <?php else: // Member not logged in ?>

            Guest Home : Sign up

            <?php endif  ?>
  6. isabel

    isabel New Member

    Joined:
    Dec 20, 2011
    Messages:
    5
    Alexander, Thanks for your suggestions and code!
    I did get the results I was looking for with your code above.
    I may have to request support to have you review our aM +WP setup - aM's still (not really) logging members into WP.
  7. nobwaart

    nobwaart New Member

    Joined:
    Jan 20, 2012
    Messages:
    20
    Hi,

    Would it be possible to expand on this slightly please?

    Basically, the above code is exactly what I was looking for. However, would the same code work in Xenforo?

    Also, what would the code be for username, so when the user is logged in, I could write something like Welcome back {username here}
  8. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279

Share This Page