# Displaying a Gravatar using a user's email in WordPress posts and pages

> Show a user's Gravatar in WordPress posts and pages with a shortcode built from a small PHP function.

Source: https://helpcenter.ruachost.com/wordpress/guides/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.

## Why Use Gravatars?

- Provides a consistent identity across websites.
- Enhances community interaction by showing user profile images.
- Adds a professional look to comments, posts, and author sections.

## Steps to Add Gravatar Display

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.

## ⚙️ Using the Shortcode

- 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.

> **Note:**
- If the email address is not registered with Gravatar, a default icon will be displayed.
- Always back up your theme files before editing.
- Consider using a **child theme** to preserve changes during theme updates.
