Default Avatar url
#1
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?
Random Fish and Sims Maniac
MY PLUGINS
Help MyBBSupport help you - remember to mark your threads as solved


Reply
#2
You can use the actual default avatar path from settings:
$avatar = $mybb->settings['bburl'] . '/' . $mybb->settings['useravatar'];
devilshakerz.com/pgp (DF3A 34D9 A627 42E5 BC6A 6750 1F2F B8AA 28FF E1BC) ▪ keybase.io/devilshakerz
Reply
#3
Perfect, thank you very much Smile
Random Fish and Sims Maniac
MY PLUGINS
Help MyBBSupport help you - remember to mark your threads as solved


Reply
#4
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.
Reply
#5
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)