1

I would like to display a nickname instead of username/display name on top of my website. I want to insert PHP codes which can display that Nickname in a PHP page. I have tried these codes below, but they don't work:

........some codes above...................
<?php global $current_user; wp_get_current_user(); ?>

{if is_user_logged_in()}
    <?php
        $nickname = $current_user->user_nickName;
    ?>

    <?php echo $nickname; ?>
    ..........some codes below.........

But it works for username as below:

........some codes above...................
<?php global $current_user; wp_get_current_user(); ?>

{if is_user_logged_in()}
    <?php
        $username = $current_user->user_login;
    ?>

    <?php echo $username; ?>
    ..........some codes below.........

Any suggestion is very appreciated!

2
  • edit your question to explain how you create the nickname.
    – mmm
    Apr 29, 2018 at 17:57
  • The user nickname is part of core WP. Apr 29, 2018 at 17:58

1 Answer 1

3

Because the $current_user object doesn't have a property called user_nickName because the user nickname is stored in the user meta.

$user = wp_get_current_user();
$meta = get_user_meta( $user->ID );
$nickname = $meta->nickname;

Or

$nickname = get_user_meta( wp_get_current_user()->ID, 'nickname', true );
5
  • Thank you for your help. It is working now with my above codes this is because i was stupid to update the file on the main theme; not the child theme.
    – daro2013
    Apr 30, 2018 at 10:53
  • here is the working code: <?php global $current_user; $current_user = wp_get_current_user(); ?> {if is_user_logged_in()} <?php $my_display_name = $current_user->nickname; ?> which i can now use that variable $my_display_name to display
    – daro2013
    Apr 30, 2018 at 10:57
  • Thanks so much Nathan Johnson. I have tried it and it works well. Dec 14, 2020 at 13:33
  • Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review
    – Sven
    Dec 14, 2020 at 14:00
  • You can also upvote to show your thanks
    – Tom J Nowell
    Dec 14, 2020 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.