Search code examples
phpwordpresswoocommerceaccountuserinfo

Change "My Account" to "Username" WooCommerce sidebar


I want to display for users once logged in instead of the default word My Account I want to display the user's name, I tried this code but it doesnt display anything!

It seems it doesn't recognized the variable $current_userin the file located at: wp-content/themes/themeName/framework/functions/woo-account.php

printf( __( '%s', 'wpdance' ),$current_user->user_lastname);

it was:

printf( __( 'My Account', 'wpdance' ));

And I tried also to get every thing using this code:

<?php global $current_user;
  get_currentuserinfo();

  echo 'Username: ' . $current_user->user_login . "\n";
  echo 'User email: ' . $current_user->user_email . "\n";
  echo 'User level: ' . $current_user->user_level . "\n";
  echo 'User first name: ' . $current_user->user_firstname . "\n";
  echo 'User last name: ' . $current_user->user_lastname . "\n";
  echo 'User display name: ' . $current_user->display_name . "\n";
  echo 'User ID: ' . $current_user->ID . "\n";

?>

But User first name: and User last name: were empty!

Does someone have any suggestion or idea?

Thank you in advanced!


Solution

  • The best way is to use wp_get_current_user() (no need of any global variable) and a conditional to be sure that the user is logged in:

    if ( is_user_logged_in() ) {
        $user_info = wp_get_current_user();
        $user_last_name = $user_info->user_lastname;
        printf( __( '%s', 'wpdance' ), $user_last_name );
    }
    

    Or with complete name:

    if ( is_user_logged_in() ) {
        $user_info = wp_get_current_user();
        $user_complete_name = $user_info->user_firstname . ' ' . $user_info->user_lastname;
        printf( __( '%s', 'wpdance' ), $user_complete_name );
    }
    

    References: