3 ways to add author’s image in WordPress

Add author’s image in WordPress without a plugin

April 5, 2017

WordPress uses Gravatar service to show the picture of authors and users, this is why there isn’t a setting field to fill in with the image of the user, but you have to go to Gravatar site and upload a picture there.

Sometimes this is not possible because the author of the post and legit owner of the email address doesn’t want to upload its photo on Gravatar and you can’t do it for him.

So here are 3 possible workarounds to change user avatar in WordPress:

Add an image in media library and use that image with get_avatar

You can add an image in media library, add a field in the edit user page to receive the address of the picture uploaded and hook the get_avatar function to use that new user field for the image. If the image hasn’t been uploaded fall back on normal get_avatar function. You can do all these things by adding this piece of code to your functions.php file:

add_filter( 'get_avatar','get_local_avatar' , 10, 5 );

function get_local_avatar($avatar, $author, $size, $default, $alt) {
  // ------------------------------------
  // handle user passed by email or by id
  if(stristr($author,"@")) $autore = get_user_by('email', $author);
    else $autore = get_user_by('ID', $author);

  $url = get_the_author_meta( 'userpicprofile', $autore->ID);
  if($url) {
    return "<img class='avatar' alt=\"".$alt."\" src='".$url."' width='".$size."' />";
  } else {
    return $avatar;
// --------------------------------------
// add the field in your user edit profile page
function add_author_image( $contactmethods ) {
  $contactmethods['userpicprofile'] = 'URL for profile image';
  return $contactmethods;


Modify the get_avatar method to use another automatic generated avatar from Flathash

Flathash is a nice free service that provides flat avatars generated with an algorithm. They are always different if you send a different parameter string to generate the avatar. So you can use the WordPress user name to generate a different avatar for each user. Here is the code to add in your functions.php file to generate fresh new avatars for your users:

add_filter( 'get_avatar','get_flathash_avatar' , 10, 5 );
function get_flathash_avatar($avatar, $author, $size, $default, $alt) {
  // -------------------------------------------
  // handle user passed by email or by id or comment obj
    $code = $author->comment_author_email;
  } else {
    if(stristr($author,"@")) $code = $autore;
      else {
        $autore = get_user_by('ID', $author);
        $code =$autore->user_email;

  // use flathash avatar instead of gravatar
  $avatar = "http://flathash.com/". md5($code);

  return "<img class='avatar' alt=\"".$alt."\" src='".$avatar."' width='".$size."' />";

Create a dedicated dir in wp-content to store the authors’ images

You can create a new folder in your /wp-content dir, so you have /wp-content/authors/ folder.
You can put in this directory all the images of your authors, named with the user nice name, so, if you have a user named “barack obama” and its author url is http://www.yoursite.com/author/barack-obama, then the nice name is barack-obama, and its jpg file will be barack-obama.jpg.

You can then add this code to your functions.php to modify the get_avatar method to first search the author image file from your new wp-content authors directory and use Gravatar service only if you can’t find the image in the local content folder:

add_filter( 'get_avatar','get_content_avatar' , 10, 5 );
function get_content_avatar( $avatar, $author, $size, $default, $alt) {
  // ------------------------------------
  // handle user passed by email or by id
  if(stristr($author,"@")) $autore = get_user_by('email', $author);
    else $autore = get_user_by('ID', $author);

  // use file with author nice name jpg in /wp-content/authors/ folder
  if(isset($autore->user_nicename) && file_exists(WP_CONTENT_DIR . '/authors/'.$autore->user_nicename.".jpg")) {

    $avatar = content_url() . '/authors/'.$autore->user_nicename.".jpg";
  } else {
    return $avatar;

It’s also possible to make a mix of these methods, for example, use a local folder to search for user pics and, if not found, use the Flathash generated avatar instead of the Gravatar one.


I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.


A new avatar generator for WordPress with get_avatar and Flathash

Brand new avatars for WordPress Gravatar service

April 7, 2017

WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

November 14, 2019

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

November 7, 2019

WordPress Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

How I've beat the php mysterious fatal error unknown in Wordpress caused by caching plugin WP Fastest Cache

WordPress Gutenberg doesn’t parse Istagram oembed url

Sometimes Instagram embed doesn't work, why?

November 4, 2019