Replace Gravatar with img URL for avatars

I have many, many WordPress authors. For each, I have used the custom wp_usermeta profile field “photo_url” to store URLs for different remotely-hosted avatar images.

I am currently displaying these image avatars on author.php by echoing the strings through the img HTML tag. But I’d like to more closely integrate them with WordPress, by making them available through get_avatar, as though they were standard avatars.

I have looked at several threads here, including on disabling Gravatar. That’s good, but how do I then make sure the URLs can be accessed through all standard WordPress avatar hooks in places I want to – ie. on author profiles, posts and loops?

(I also want to default to get_template_directory_uri() . '/images/avatar_default.png' whenever the “photo_url” has no value.

Thank-you.

1 Answer
1

Unless I have misunderstood your question to accomplish a custom gravatar to use in your theme add the code below to your functions.php or into a custom plugin.

From there customize the title and the image you want to use. See screenshot below for the finished outcome.

add_filter( 'avatar_defaults', 'dev_designs_gravatar' );
/**
 * Display a custom Gravatar
 * 
 * @param       $avatar
 * @return      mixed
 * @author      Joe Dooley - [email protected]
 *              
 */
function dev_designs_gravatar( $avatar ) {
    $custom_avatar            = get_stylesheet_directory_uri() . '/images/avatar_default.png';
    $avatar[ $custom_avatar ] = "Custom Gravatar";

    return $avatar;
}

See Custom Avatar in top right of the admin bar.
Select new Custom Gravatar option to set default gravatar for theme.

Leave a Comment