I have been trying to use Gravator's Identicon as the fallback image, is there is no image for a given user in a predefined image folder.

consider the following scenario;

User email : asd@asd.com

an image is uploaded to wp-content/images/users/ with the file name 0eb178cec364c022a189c3814e5f7483.jpg (The file name is generated with md5("asd@asd.com"."customkey");)

If an imaage file is available as wp-content/images/users/0eb178cec364c022a189c3814e5f7483.jpg, I need to show that image as the users avatar. If there is no such image, Gravatar will be shown.

So far I have tried using the Gravatar as the background, but there is a broken image icon shown if a custom image is not available.

I am looking for non-plugin solution as all the plugins use default uploads folder and create many thumbnails even for this purpose, which is fully unnecessary. I couldn't find any plugins which tick all the boxes. Any help is appreciated.

  • Also It is not practical to gave Gravatar loaded custom images for all users as all of them have a company email address, and not their personal email addresses. – Sid Jul 20 '17 at 15:21

You can use the get_avatar_url filter (see the arguments passed here in the source code) to change the avatar url and then simply use get_avatar() with the user email in the theme as you're used to.

add_filter('get_avatar_url', 'wpse_avatar_or_gravatar', 10, 3);
function wpse_avatar_or_gravatar($url, $id_or_email, $args) {
    // was id passed via $id_or_email
    if ($id_or_email == intval($id_or_email)) {
        $userdata = get_userdata($id_or_email);
        $email = $userdata->user_email;
    // was email passed via $id_or_email
    else {
        $email = $id_or_email;

    $path = sprintf('%s/images/users/%s.jpg',
                md5($email . "customkey")

        // image exists, return url
        if (file_exists($path)) {
                $url = sprintf('%s/images/users/%s.jpg',
                    md5($email . "customkey")
        else {
                $url = "https://0.gravatar.com/avatar/" . md5($email . 'customkey') . "?s=64&d=identicon&f=y&r=g";
        // image does not exist, return default avatar
        return $url;
  • Thanks. It works fine. I added global $current_user; and $current_user = wp_get_current_user(); the get the current user's email address. Also changed to get_avatar_url as the above function only gave a text link rather than an image. But when I view the users page, all users have the current user's avatar. How can I display individual avatars separately with the above? – Sid Jul 21 '17 at 4:14
  • Yes, I meant to use get_avatar_url as well. This happens because yo use $current_user. I will update the answer – kero Jul 21 '17 at 10:18
  • The code I have modified is in here> gist.github.com/anonymous/551f22507b7edf2be1b70d4c5a9f86c9 I had to edit the core pages to force them to use "email" address rather than user ID when generating md5 hash. Also the images are stored in /wp-content/images. Uploads folder is not used. – Sid Jul 21 '17 at 10:34
  • Never edit core WordPress files (ie those files in /wp-admin/includes/ or /wp-includes/)! I have updated again to use the wp-content constants instead of the upload dir, must have misread that, sorry. – kero Jul 21 '17 at 10:39
  • 1
    My thoughts exactly! But nah. Nothing I could think of. Tx for the support! :D – Sid Jul 22 '17 at 10:06

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.