Display User or Guest Gravatar and Nickname with a Shortcode in WordPress
By Scot Birchfield
284 days agoJun . 2024

Effortlessly display user-specific or guest Gravatar icons and nicknames on your WordPress pages and posts using a simple shortcode. This functionality seamlessly integrates with any WordPress theme, including popular ones like Kadence and Astra. With this approach, logged-in users will see their Gravatar and nickname, while visitors will see a default “Guest” icon and name. Leveraging FluentSnippets for all custom PHP, CSS, JS, and HTML coding ensures a smooth and efficient implementation process.


I use fluent snippet plugin for all my functions, js, and CSS:
Detailed Example
To display user-specific or guest Gravatar icons and nicknames on your WordPress site, add the following PHP code to your FluentSnippets setup:
Add the following CSS to your FluentSnippets setup to control the size of the Gravatar icon:
.user-gravatar-nickname {
display: inline-flex;
align-items: center;
}
.user-gravatar-nickname .user-gravatar {
width: 20px;
height: 20px;
border-radius: 50%;
margin-right: 5px; /* Adjust spacing between the image and nickname */
}
How to Use the Shortcode
- Default Usage: Use the shortcode to display the Gravatar and nickname of the currently logged-in user, or “Guest” and a generic icon if not logged in.
- Specify a User ID: Use the shortcode with a specific
user_id
attribute to display the Gravatar and nickname of a specific user. See the Example below to display the Gravatar and nickname for the user with ID 1.
To display a general user:
Guest
To display a specific user:
Webmaster
This shortcode ensures that any WordPress site, regardless of the theme, can dynamically display user-specific or guest Gravatar icons and nicknames with ease.
Comments: