Skip to content

Displaying a gravatar using user email in WordPress posts and pages

A Gravatar (Globally Recognized Avatar) is an image associated with a user’s email address that appears across websites. WordPress has built‑in support for Gravatars, and you can display them in posts and pages using a shortcode. On ruachost.com, this can be achieved by adding a simple PHP function.

  • Provides a consistent identity across websites.
  • Enhances community interaction by showing user profile images.
  • Adds a professional look to comments, posts, and author sections.
  1. Log in to WordPress with an administrator account.

  2. In the dashboard, go to Appearance → Theme Editor.

  3. From the right‑hand column, select the file named functions.php.

  4. Scroll to the bottom of the file and add the following code:

    function wpb_display_gravatar($atts) {
    extract(shortcode_atts(array(
    'wpb_user_email' => ''
    ), $atts));
    if ($wpb_user_email == '') {
    global $current_user;
    get_currentuserinfo();
    $getuseremail = $current_user->user_email;
    } else {
    $getuseremail = $wpb_user_email;
    }
    $usergravatar = 'http://www.gravatar.com/avatar/' . md5($getuseremail) . '?s=64';
    return '<img src="' . $usergravatar . '" />';
    }
    add_shortcode('wpb_gravatar', 'wpb_display_gravatar');
  5. Click Update File to save changes.

  • To display the Gravatar of the current logged‑in user:

    [wpb_gravatar]
  • To display the Gravatar of a specific email address:

    [wpb_gravatar wpb_user_email="john.smith@example.com"]

✅ The Gravatar will appear as an image in your post or page.