27 September 2014 ~ 0 Comments

How to Add Gravatars to PHPBB.. the easy way!

I’ve spent the better part of this afternoon figuring out an easy way to add gravatars to the Team Conquets forum.. there was a serious lack of information about it so I figured I’d make a post explaining simply how you can do it by changing the avatar function to gravatar…

The first step is to go to viewtopic.php in your forum directory..

You will find this at or around line 1156

‘avatar’ => ($user->optionget(‘viewavatars’)) ? get_user_avatar($row[‘user_avatar’], $row[‘user_avatar_type’], $row[‘user_avatar_width’], $row[‘user_avatar_height’]) : ” ,

Just simply replace it with:

‘avatar’ => ($user->optionget(‘viewavatars’)) ? get_user_avatar($row[‘user_avatar’], $row[‘user_avatar_type’], $row[‘user_avatar_width’], $row[‘user_avatar_height’], ‘USER_AVATAR’, $row[‘user_email’]) : ” ,

The next step is to find your functions_display.php file in the forums include folders, it will be at or around line 1302

function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = ‘USER_AVATAR’)
{

global $user, $config, $phpbb_root_path, $phpEx;

if (empty($avatar) || !$avatar_type)
{

return ”;

}

}

Now replace that with this Gravatar function

function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = ‘USER_AVATAR’, $user_email = ”)
{

global $user, $config, $phpbb_root_path, $phpEx;

if (empty($avatar) || !$avatar_type)
{

if ($user_email) {

$grav_id = md5($user_email);
$grav_default = ‘mm’;
$grav_rating = ‘R’;
$grav_size = 100;

$grav_url = “http://www.gravatar.com/avatar.php?gravatar_id=$grav_id&default=$grav_default&size=$grav_size&rating=$grav_rating”;

return ‘<img alt=”‘ . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . ‘” src=”‘ . $grav_url . ‘” width=”‘ . $grav_size . ‘” height=”‘ . $grav_size . ‘” />’;

}

else {

return ”;

}

}

}

That return string is your output, you may wish to add Center tags or css to it.

The default is any of the Gravatar default images, you can change that variable to whichever you prefer, including the URL to an image.. the size is the pixel size, and the rating is the maximum rating you’ll allow to be displayed.

Enjoy 🙂