MyBB Community Forums

Full Version: Default Avatar url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on a plugin, and I have this code to display avatars:

if (!empty($referral['avatar'])){
			
$avatar = htmlspecialchars_uni($referral['avatar']);
				
	} else {
			
$avatar= $mybb->settings['bburl'].'/images/default_avatar.png';
}

But if the default avatar path is different to default (or it isnt a png) then the image link will break. How would I make that default avatar link work with a custom path to a default avatar?
You can use the actual default avatar path from settings:
$avatar = $mybb->settings['bburl'] . '/' . $mybb->settings['useravatar'];
Perfect, thank you very much Smile
Also, if you're using 1.8, you can make things even easier using MyBB::get_asset_url():

$avatar = htmlspecialchars_unj($mybb->get_asset_url($mybb->settings['useravatar']));

This will also take into account CDN settings and such.
Or, even simplier and properly scaled, use format_avatar() in 1.8 which was made exactly for this:
$avatar = format_avatar($referral['avatar'], $referral['avatardimensions']]); // also select avatardimensions column in your query, you can provide 3rd argument for max dimensions other than default
echo $avatar['image']; // src
echo $avatar['width_height']; // width and height attributes