Family Encyclopedia >> Electronics

How to Display Gravatar Avatars from User Emails in WordPress: Templates, Posts & Widgets

As seasoned WordPress developers, we've relied on Gravatar for years to effortlessly display user avatars across our sites. Most commonly used in comments, Gravatar works anywhere you need a profile image—like author bios, sidebars, or about pages. In this practical guide, we'll walk you through displaying Gravatar avatars using any user email address.

How to Display Gravatar Avatars from User Emails in WordPress: Templates, Posts & Widgets

Note: Our example draws from a membership site, fetching the email from registered user data. The core technique applies universally, whether for logged-in users or any email.

Display Gravatar from User Email in WordPress Theme Files

To integrate Gravatar into your theme templates, add this reliable function to your theme's functions.php file or a custom plugin. It pulls the current user's email and generates the avatar URL securely.

function wpbeginner_display_gravatar() {
    global $current_user;
    get_currentuserinfo();
    $getuseremail = $current_user->user_email;
    $usergravatar = 'https://www.gravatar.com/avatar/' . md5( strtolower( trim( $getuseremail ) ) ) . '?s=32&d=mm&r=g';
    echo '<img src="' . esc_url( $usergravatar ) . '" alt="User Gravatar" />';
}

Call it in any template file like this: <?php wpbeginner_display_gravatar(); ?>

Display Gravatar from User Emails in Posts, Pages, and Widgets

Need to show Gravatar for any email—even non-registered users—or specific ones in content? Use this shortcode. Add it to functions.php or a custom plugin:

function wpb_display_gravatar( $atts ) {
    $atts = shortcode_atts( array(
        'wpb_user_email' => '',
    ), $atts );
    if ( empty( $atts['wpb_user_email'] ) ) {
        global $current_user;
        get_currentuserinfo();
        $getuseremail = $current_user->user_email;
    } else {
        $getuseremail = $atts['wpb_user_email'];
    }
    $usergravatar = 'https://www.gravatar.com/avatar/' . md5( strtolower( trim( $getuseremail ) ) ) . '?s=32&d=mm&r=g';
    echo '<img src="' . esc_url( $usergravatar ) . '" alt="Gravatar" class="wpb-gravatar" />';
}
add_shortcode( 'wpb_gravatar', 'wpb_display_gravatar' );

This shortcode defaults to the current user but accepts a custom email. Examples:

[wpb_gravatar] – Shows current user's Gravatar.

[wpb_gravatar wpb_user_email="admin@989214.com"] – Displays Gravatar for the specified email.

Style it with this CSS in your stylesheet:

.wpb-gravatar {
    padding: 3px;
    margin: 3px;
    background: #FFFFFF;
    border: 2px solid #eee;
}

We've tested these methods on live sites, ensuring compatibility and security. Questions? Drop a comment below—we're here to help.