Hotaru Docs


Showing User Avatars

Including user avatars in your theme is easy if you know the ID number of the user you want to show.

First, here are three ways to get a user's ID number:

$id = $h->currentUser->id; // this holds the ID of the person viewing the page
$id = $h->getUserIdFromName($username); // provide the username
$id = $h->getUserIdFromEmail($email); // provide the email address

Next, you need to set the avatar, like this:

$h->setAvatar($id, $size, $rating);
  1. $size defaults to 32 pixels
  2. $rating g, pg, r, or x. The default is "g" (safe for kids).


$h->setAvatar($id); // defaults to 32 pixels with rating "g".
$h->setAvatar($id, 16); // 16 pixels with rating "g".
$h->setAvatar($id, 16, 'pg'); // 16 pixels with rating "pg".
$h->setAvatar($id, 0, 'x'); // default 32 pixels with rating "x";

To show the avatar, you have three choices:

echo $h->getAvatar(); // displays the avatar, i.e. IMG tags only
echo $h->linkAvatar(); // displays the avatar linked to the user's profile
echo $h->wrapAvatar(); // displays the avatar linked and wrapped in a div with class "avatar_wrapper"

Before you show an avatar, it's a good idea to check if an avatar plugin (e.g. Gravatar) is currently active:

if($h->isActive('avatar')) {
    $h->setAvatar($h->currentUser->id, 16);
    echo $h->linkAvatar();

Advanced usage

The above code works fine for a handful of avatars, but it might be a bit slow with the Gravatar plugin because Gravatar requires a user email and we don't provide that from the template. Instead, the Avatar class in Hotaru's libs directory pulls the user details for each user from the database (if not cached) and the Gravatar plugin then gets the email address from those.

If you already have the user's id, username and email address, a faster way (although more wordy) is to get the avatar like this:

$avatar = new Avatar($h);
$avatar->user_id = $id;
$avatar->user_email = $email;
$avatar->user_name = $name;
$avatar->size = 16;  // optional - defaults to 32 if not used
$avatar->rating = "pg"; // optional - defaults to "g" if not used
$avatar->setVars($h); // this is so plugins can access the properties
echo $avatar->getAvatar($h); // or linkAvatar($h) or wrapAvatar($h)

I would recommend using code like that if you want to show a block filled with user avatars.

Getting StartedDesign and LayoutPlugin DevelopmentAdvanced TopicsFunction ReferenceTroubleshooting