WordPress.org

Ready to get started?Download WordPress

Forums

Getting User Identity Function To Work On header.php (7 posts)

  1. jaruff
    Member
    Posted 6 years ago #

    Here is my situation. I am currently working on a custom theme and there is one issue that I cannot seem to resolve.

    Basically, I want to use the user_identity function on the header.php file for a user bar.

    Here is my code:

    <!-- User Start -->
    
    <?php if ( $user_ID ) : ?>
    
    Welcome <b><?php echo $user_identity; ?></b>!
    ( <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout">Log Out</a> | <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php">Profile</a><?php wp_register(); ?> )
    
    <?php else : ?>
    
    Welcome <b>Guest</b>!
    ( <a href="<?php echo get_option('siteurl'); ?>/wp-login.php">Log In</a><?php wp_register(); ?> )
    
    <?php endif; ?>
    
    <!-- User End -->

    I know that my coding isn't the most optimized but that wouldn't be the reason for the function to not work.

    I would be most grateful if someone could assist me in resolving this issue.

  2. culvejc
    Member
    Posted 6 years ago #

    I found your post on Google, and figured out a painfully simple solution on my own.
    <!-- User Start -->

    <?php if ( $user_ID ) :

    // here's the kicker
    global $user_identity;
    // really. that's it. ?>

    Welcome <b><?php echo $user_identity; ?></b>!
    ( <a href="/wp-login.php?action=logout">Log Out</a> | <a href="/wp-admin/profile.php">Profile</a><?php wp_register(); ?> )

    <?php else : ?>

    Welcome <b>Guest</b>!
    ( <a href="/wp-login.php">Log In</><?php wp_register(); ?> )

    <?php endif; ?>

    <!-- User End --></a>

    Easy enough, right? Cheers.

  3. evermail
    Member
    Posted 6 years ago #

    THIS is what I was looking for. Thanks!

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    This works pretty much anywhere:

    $user = wp_get_current_user();
    if ($user) {
    //user is logged in, $user->ID will be their ID, etc..
    var_dump($user); // shows everything about them and all their variables
    }
  5. jmaentz
    Member
    Posted 6 years ago #

    This works great, but it doesn't seem to work in the header.php if I have an embedded Gallery2 in that page (using WPG2). It seems to think no one is logged in, even though they are. Any suggestions? It works fine if it is simply a wordpress page without the embedded content. I'm trying to figure a way around this.

  6. karnesb
    Member
    Posted 6 years ago #

    culvejc's string works great for me, too. I was just wondering how to modify it so it only display's the user's first name instead of their full name.

    Thoughts?

  7. culvejc
    Member
    Posted 6 years ago #

    For whiner karnesb, after DAYS of email harassment ;)

    $user_info = get_userdata($user_ID);
    echo $user_info->first_name;

    Cheers.

    Jon

Topic Closed

This topic has been closed to new replies.

About this Topic