1

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!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
MiraTech
  • 1,108
  • 18
  • 35

2 Answers2

1

Try calling

global $current_user;
get_currentuserinfo();

before

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

See https://codex.wordpress.org/Function_Reference/get_currentuserinfo#Examples

And are you sure the lastname is always set? Probably you can make sure $current_user works, if $current_user->ID at least returns a value.

And enable debugging in your wp_config.php might help as well to display all notices and errors:

define( 'WP_DEBUG', true );

See https://codex.wordpress.org/Debugging_in_WordPress

Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
  • Thanks for your answer, I tried it and had this error: `Notice: The called constructor method for WP_Widget in mgwoocommercebrands_list_widget is deprecated since version 4.3.0! Use __construct() instead. in /home/classi33/public_html/wp-includes/functions.php on line 3718` Do you have any idea? – MiraTech May 30 '16 at 15:17
  • And I think that has nithing to do with it! – MiraTech May 30 '16 at 15:31
1

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:

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for sharing your answer but it doesn't display anything! – MiraTech May 30 '16 at 14:42
  • The error is: `Notice: La méthode du constructeur appelée pour WP_Widget dans mgwoocommercebrands_list_widget est obsolète depuis la version 4.3.0 ! Utilisez __construct() à la place. in /home/epicerymarket/public_html/wp-includes/functions.php on line 3718` Do you have any idea? – MiraTech May 30 '16 at 15:01
  • I got you, you are right, your code is corret! so I have another issue I will post new question about it! – MiraTech May 30 '16 at 16:04
  • @AbdelazizMirad You are not using the right way to make your widget since wordpress 4.3. But this is an other question (Une autre question). Here you have a solution in french: [Notice: WP_Widget is deprecated since version 4.3.0](https://wordpress.org/support/topic/notice-wp_widget-is-deprecated-since-version-430). Thank you :) – LoicTheAztec May 30 '16 at 16:05