How to Set a Custom Default Gravatar Image in WordPress

Home » Snippets » How to Set a Custom Default Gravatar Image in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wondered how to customize the default Gravatar image on your WordPress website? By default, WordPress uses a generic, anonymous image as the default avatar for users who do not have a custom profile picture. However, you may want to personalize this default image to align with the branding and aesthetic of your website. In this article, we will guide you through the process of setting a custom default Gravatar image for your WordPress site, allowing you to add a personal touch to the user experience.

					function wpturbo_custom_default_gravatar($avatar){
    $custom_image = 'https://example.com/path/to/custom-image.png';
    $avatar = '<img src="'. $custom_image .'" alt="Custom Gravatar" class="avatar avatar-96 photo" height="96" width="96" />';
    return $avatar;
}
add_filter('avatar_defaults', 'wpturbo_custom_default_gravatar');
				

The code snippet provided demonstrates how to set a custom default Gravatar image instead of using the default images provided by WordPress.

To achieve this, we need to create a new function called wpturbo_custom_default_gravatar(). This function will be responsible for returning the HTML markup that represents the custom Gravatar image.

Inside the function, we define the URL of the custom image by assigning it to the variable $custom_image. You need to replace 'https://example.com/path/to/custom-image.png' with the URL of your own custom image. Make sure to use a valid image URL.

Next, we create the HTML code for the custom Gravatar image. In this case, we use the <img> tag to display the image. The source (src) attribute is set to the value of the $custom_image variable, which points to the URL of the custom image. The alt attribute is set to "Custom Gravatar" to provide alternative text for the image. The class attribute is set to "avatar avatar-96 photo" to apply the appropriate styling for an avatar image. The height and width attributes are set to "96" to specify the dimensions of the image.

Finally, we return the HTML markup of the custom Gravatar image by assigning it to the variable $avatar and then using the return statement.

To apply the custom default Gravatar image, we need to hook the wpturbo_custom_default_gravatar() function into the avatar_defaults filter using the add_filter() function. This filter is responsible for modifying the default avatars used by WordPress.

Once the filter is added, WordPress will use the custom Gravatar image specified in the wpturbo_custom_default_gravatar() function as the default image for user avatars.

That’s it! By using this code snippet and providing the URL of your custom image, you can easily set a custom default Gravatar image in WordPress.

Register an account to save your snippets or go Pro to get more features.