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?
Section titled “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
Section titled “Steps to Add Gravatar Display”-
Log in to WordPress with an administrator account.
-
In the dashboard, go to Appearance → Theme Editor.
-
From the right‑hand column, select the file named functions.php.
-
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'); -
Click Update File to save changes.
⚙️ Using the Shortcode
Section titled “⚙️ 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.