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:
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.
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.
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.
December 10, 2012 at 5:02 pm
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.
December 10, 2012 at 11:49 pm
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.
December 11, 2012 at 10:00 am
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
January 24, 2013 at 1:58 pm
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.
January 25, 2013 at 9:34 am
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.