address-book

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

| 5 Comments

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).

// DISPLAY USER'S FIRST NAME

function user_first_name() {
	global $current_user;
	get_currentuserinfo();
	$first_name = esc_attr( $current_user->user_firstname );
	return $first_name;
}

add_shortcode( 'user_first_name', 'user_first_name' );

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

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Keep your site backed up, updated & secure

I provide a service called The WP Butler, which helps you stay on top of the maintenance of your WordPress site. Instead of worrying about whether your site is secure, updated and backed up, The WP Butler handles all that for you on a regular basis, so that you can focus on doing what you do best. If you use coupon DIWW, you'll save 15% on our already-low-prices for all maintenance plans.

Author: Dave Clements

Dave Clements has been building websites for close to a decade and in 2010, he formalised that by starting his own company, The UK Edge. He now works on a variety of web projects, from simple tasks like installing a new WordPress site, to consulting on problems, or redesigning his clients' sites. He also runs Do It With WordPress, a site dedicated to providing free tutorials on WordPress. When he's not building your new website, you can find Dave eating Wheat Thins, spending time with friends and family, watching Indie films, fostering kittens from the local Humane Society, listening to some dubstep, dance and electronic rock, and exploring the world.

5 Comments

  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.

    • 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.

      • 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. 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.

    • 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