[Resolved] Add User Name in Header Element

Home Forums Support Add User Name in Header Element

  • This topic has 3 replies, 2 voices, and was last updated 11 months ago by David.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1042942
    Kim

    Hi there, I would like to create a header element for the woocommerce my account page displaying the customer/user name. Is this possible and how can I achieve this?

    Many thanks in advance!

    #1043170
    David
    Staff
    Customer Support

    Hi there,

    add this PHP snippet:

    function db_display_current_user() {
        ob_start();
        $current_user = wp_get_current_user();
    
        if ( is_user_logged_in() ) {
            echo '<span class="current-user">' . $current_user->display_name . '</span>';
        } else {
            echo '<span class="current-user">Message for not logged in user</span>';
        }
        return ob_get_clean();
    }
    
    add_shortcode( 'current_user_name', 'db_display_current_user' );

    Then you can use the [current_user_name] shortcode in the Header Element

    #1043888
    Kim

    Awesome! Many thanks 🙂

    #1044061
    David
    Staff
    Customer Support
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.