I’ve recently made a couple of membership websites in WordPress, which has been an experience, to say the least.
A popular feature of a typical website that allows you to login, is to have a “you are logged in as:” box, that displays your avatar, username, and often a link to edit your profile. Naturally I’ve been trying to replicate this functionality on one of the websites I am building. It came as a surprise that there is very little native avatar support in WordPress, which I think is a bit silly considering it is essentially a blogging platform that has escaped and run wild. Never the less, as is usually the case with WordPress, a bit of careful prodding reveals that there are ways around such things.
Now, I’ve not bothered to look into using a third party avatar plugin for this – I’m just going to stick with using the default Gravatar or get over it option.
So, to begin with, to get a user’s avatar outside of The_Loop, we need to refer to a global variable. Global variables are used for a number of reasons – backwards compatibility being one of the main ones, and these variables shouldn’t be over-used. They are, however, essential for this. The global variable we use will allow us to get information from the set_current_user function. Obviously if a user isn’t logged in, then the avatar will have to fall back on a default image, or you could write your code in such a way that the avatar is only displayed if a user is logged in.
So, with no wrapping code, this is how you display a user’s avatar, if a user is logged in:
< ?php global $user_email ?>
< ?php echo get_avatar($user_email, '50'); ?>
I was going to post a whole thing about writing a “logged in as” box, but I was having a problem with code being parsed, despite me asking the WYSIWYG not to parse code… Oh well.
