WordPress uses the Gravatar global avatar service by default, but its loading speed can be affected by network conditions, potentially slowing down page loads. While using SSL routes or caching plugins can partially mitigate this, users who haven't set a Gravatar will see a default gray silhouette, which is not very visually appealing.
This article introduces a code-only solution to replace Gravatar with local random avatars, improving loading speed and the visual appearance of default avatars.
Implementation Code
Add the following code to your current theme's functions.php file:
add_filter( 'get_avatar', 'custom_local_random_avatar', 1, 5 );
function custom_local_random_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$avatar_url = '';
$user_id = 0;
if ( is_numeric( $id_or_email ) ) {
$user_id = (int) $id_or_email;
} elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
$user_id = (int) $id_or_email->user_id;
} elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) ) {
$user_id = $user->ID;
}
if ( $user_id === 1 ) { // Admin gets a specific avatar
$avatar_url = get_template_directory_uri() . '/avatar/admin.jpg';
} else { // Other users get a random avatar
$random_number = mt_rand( 1, 10 );
$avatar_url = get_template_directory_uri() . '/avatar/' . $random_number . '.jpg';
}
$avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . esc_url( $avatar_url ) . "' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return $avatar;
}
Code Explanation & Setup
Core Logic
- The code hooks into WordPress's
get_avatarfilter to replace the default avatar logic. - It checks if the request is for an administrator (example uses user ID 1). If yes, it returns
admin.jpg. Otherwise, it picks a random number (1-10) and returns the corresponding[number].jpg. - The user identification logic is robust, handling user IDs, comment objects, or email addresses.
Image Preparation
- Create a folder named
avatarin your active WordPress theme's root directory. - Place 10 square images for random avatars inside, named
1.jpg,2.jpg, ...,10.jpg. - Add one specific image for the administrator, named
admin.jpg.
Note: All images should be square and optimized (compressed) for performance.
Customization
- Number of Images: Change the
10inmt_rand( 1, 10 )to match your actual number of images. - Admin Check: The example checks for
$user_id === 1. For multiple admins or a different ID, modify this condition. Usingcurrent_user_can( 'manage_options' )is a more universal role-capability check. - Image Format: The code uses
.jpg. Change the file extension in the code if your images are PNG or WebP.
Result & Important Notes
After enabling this:
- All visitors and non-admin users without a Gravatar will see a randomly assigned avatar in comments and profiles.
- The administrator (or your specified user) will always see the fixed
admin.jpg. - This method bypasses Gravatar entirely, improving avatar load speed and eliminating missing avatars due to Gravatar inaccessibility.
Important: Always back up your theme files before making changes. It's recommended to add this code to your child theme's functions.php to prevent overwrites during theme updates.