6 Aug 2019 #1

Default avatar with Hovercards

Ultimate subscriber
@Shade Great job. However, how to set a default avatar if user has none?
Shade 6 Aug 2019 Edited
#2
Hovercards does not come with this ability. There are plenty of plugins that handle it though. I suggest you to use Letter Avatars 1.0.2 which adds some style to default avatars.
ProX 6 Aug 2019
#3
Hovercards does not come with this ability. There are plenty of plugins that handle it though. I suggest you to use Letter Avatars 1.0.2 which adds some style to default avatars.
Shade (6 Aug 2019)
Should this already fix it?
function defaultavatarfix()
{
    global $mybb;

    if(!$mybb->user['avatar'] && !empty($mybb->settings['useravatar']))
    {
        $mybb->user['avatar'] = $mybb->settings['useravatar'];
    }
}
$plugins->add_hook("global_intermediate", "defaultavatarfix");
Shade 6 Aug 2019
#4
This does not work with Hovercards as it's just a local fix. Letter Avatars 1.0.2 and all related plugins store the default avatar in the database which is the source from which Hovercards pulls data from.
ProX 6 Aug 2019
#5
This does not work with Hovercards as it's just a local fix. Letter Avatars 1.0.2 and all related plugins store the default avatar in the database which is the source from which Hovercards pulls data from.
Shade (6 Aug 2019)
Just checked the code, seems like avatars are handled by javascript.

Basically the first statement checks if the avatar contains link from the db? If yes, it uses google proxy to show image. Else it returns empty avatar, right? So we can just define user.avatar?
if (user.avatar.indexOf('http') > -1) {
                        image.src = 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&refresh=300&url=' + encodeURIComponent(user.avatar);
                    } else {
                        image.src = user.avatar;
                    }
Shade 6 Aug 2019 Edited
#6
Nope, it just checks if it’s an uploaded avatar or a remote one. Remotes have to pass through Google to get adaptive colors due to CORS restrictions. Either way modifying Hovercards is a hassle, much simpler to use plugins for such things.
ProX 6 Aug 2019
#7
Nope, it just checks if it’s an uploaded avatar or a remote one. Remotes have to pass through Google to get adaptive colors due to CORS restrictions. Either way modifying Hovercards is a hassle, much simpler to use plugins for such things.
Shade (6 Aug 2019)
The letter avatar adds remote api which I really don't like. So will have to edit it manually. Can you just tell me which function checks if avatar exists? I've tried with negative Hovercards.exists(user.avatar), but doesn't seem to be working.
Shade 6 Aug 2019
#8
It's not in the JS, but in the database, so you'll have to hack inc/plugins/hovercards.php. If you don't like external APIs, you can look for default avatar plugins on the community forums, there are plenty.
ProX 6 Aug 2019 Edited
#9
It's not in the JS, but in the database, so you'll have to hack inc/plugins/hovercards.php. If you don't like external APIs, you can look for default avatar plugins on the community forums, there are plenty.
Shade (6 Aug 2019)
Something like this?
if (empty($args[9])) // position of avatar column in db
return '/images/default_avatar.png';
Setting default avatar for hundreds of thousand members will do overkill. Even if limited it would take lots of time.
Shade 6 Aug 2019 Edited
#10
inc/plugins/hovercards.php, line 388:

$users[] = $user
Add before:

if (!$user['avatar']) {
    
$user['avatar'] = 'what you want';

Btw, it's not overkill even for hundreds thousands of users. You could run a single query to set a default avatar, and install a plugin that takes care of new users when they register. Just my two cents.
day-day likes this post