2

Users can upload an avatar as a local image file on my website. If they didn't do so, it will fall back to the WordPress default (Gravatar). But I want any occurrence of Gravatar to be replaced with a locally stored placeholder image.

3

OK, so there are some occurrences of get_avatar() in your site. If you'll take a look at docs for this function, you'll see, that:

  • you pass URL for default avatar image as 3rd param,
  • you pass args as 5th param.

And one of these args is:

force_default (bool) (optional) Whether to always show the default image, never the Gravatar. Default: false.

And as default you can use:

URL for the default image or a default type. Accepts '404' (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 'gravatar_default' (the Gravatar logo). Default: Default is the value of the 'avatar_default' option, with a fallback of 'mystery'.

But...

if there are a lot of occurrences of get_avatar or you don't want to modify these occurrences (for example some of them come from plugins), then you can achieve this using filters.

One way to do this would be to use pre_get_avatar filter.

If you'll return any non-null value, the rest of get_avatar function will get ignored and your result will be used as avatar. So you can use something like this, to do the trick:

function replace_all_avatars_with_default_one( $html, $id_or_email, $args ) {
    $url = '<URL TO DEFAULT AVATAR';
    $class = array();
    return $avatar = sprintf(
        "<img alt='%s' src='%s' class='%s' height='%d' width='%d' %s/>",
        esc_attr( $args['alt'] ),
        esc_url( $url ),
        esc_attr( join( ' ', $class ) ),
        (int) $args['height'],
        (int) $args['width'],
        $args['extra_attr']
    );
}
add_filter( 'pre_get_avatar', 'replace_all_avatars_with_default_one', 10, 3 );
  • But this will use the remote placeholder from Gravatar, right? I need to replace all occurrences of Gravatar with my own local image file. – Florian Walther Feb 17 at 11:29
  • @FlorianWalther no, setting force_default will use the default avatar you choose. – Krzysiek Dróżdż Feb 17 at 11:34
  • Thank you for the update. But I need to keep avatars that are uploaded locally by the user. That's why I want to disable specifically images from Gravatar.com – Florian Walther Feb 17 at 11:50
  • 1
    @FlorianWalther return empty string in your filter function – Krzysiek Dróżdż Feb 17 at 12:54
  • 1
    @FlorianWalther 10 is priority. 3 is the number of params your function takes. – Krzysiek Dróżdż Feb 17 at 13:22
1

Krzysiek has a good solution, but I do this a bit differently...because I also want to allow users to upload a locally stored image and then fall back to Gravatar if they don't (or a default image if they have no Gravatar setup).

I use ACF (Advanced Custom Fields) to let authors upload an image because it easily adds the image upload function to every user's profile page, but you may be using something different - however you allow them to upload and store the image you'll need the name of the usermeta field, my example below uses the meta field name 'author_profile_picture'.

Also, I'm sure you already know this but for the benefit of others who may find this and need help, Gravatars are added by the Theme using WP's "get_avatar" function, so all you need to do is find where they're being added by the Theme and change that code. In SOME Themes, it calls code that is in a functions.php file or other includes in order to use get_avatar with a number of theme-controlled parameters, but you can just remove it entirely and replace it with new code to do what YOU want rather than what the Theme wants - just pay attention to image sizes so your styling still looks good.

I use the code in my example below to find and display my usermeta field (if it exists) OR default to the gravatar/default image if it does not......this is a graceful way to allow users to choose a locally stored image or their Gravatar, but you could also choose to display no image if you really don't want Gravatar images used.

Here's my code with notes

    <?php 

    global $user; \\ necessary to access ALL user meta fields

    $authorimage = get_the_author_meta('author_profile_picture', $user->ID );

    if ( $authorimage ) { ?>
    <img src="<?php echo $authorimage; ?>" class="authorimg" />       
    <?php
    } else { 
    echo get_avatar( get_the_author_meta( 'user_email' ) ); \\ to display the Gravatar or Default image
      \\ OR you can do something different if you DON'T want Gravatar
    echo 'no author image available'; \\ Lots of options here, you can choose to do whatever if no image has been uploaded
     } ?>

<h4>Meet The Author: <?php the_author();  ?></h4>
<div itemprop="description" class="description">
<p><?php echo get_the_author_meta('description',$user->ID); ?></p>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.