Display User’s Name, or any of Their Other Information

address-book

I put together this very simple snippet to display a user’s name when they log in to a premium membership site. Then, to display their name, all you have to do is use the shortcode [user_first_name], and their name will appear instead (assuming it has been entered in their profile).

You can take it a little further and modify this to show other information instead by taking advantage of the other variables available under $current_user. Use the following values to show more information:

  • First name: user_firstname
  • Last name: user_lastname
  • Display name: display_name
  • Username: user_login
  • User email address: user_email
  • User ID: ID

5 thoughts on “Display User’s Name, or any of Their Other Information”

  1. Shouldn’t you be returning the first name sanitized with esc_attr? That’t at least what I do for any item that comes right out of the DB.

    1. Point well taken Curtis. Sanitization is something I’m just getting my hands dirty with, so it’s not yet common practice for me. I’ll update the tutorial. Thanks again for the tip.

      1. I just really started getting in to proper sanitization 6 months ago, but now I see issues all over with it. There is always so much to learn, we can’t start knowing everything at once.

        Cheers

  2. vickych says:

    Very useful code. HTML code is better than adding widgets or plugins in a wordpress. Plugins and other extra installations slow down a website so nice and simple code for better and attractive look.

    1. Actually, the idea that lots of plugins slow your site down is a misconception. This snippet is PHP (not HTML) just like any other plugin, and including it in as its own plugin, or adding it to an existing plugin, like a functionality plugin, makes no difference to its impact on your site.

      The idea that lots of plugins slow down your site is when people install lots of badly coded plugins which can impact performance. But this small snippet could be it’s own plugin and have absolutely no impact on load time at all.

Leave a Reply