Setting a Custom Image as the Default Avatar

A brief note on how to set a custom image as the default avatar. There are at least three ways to do this. Below, you will find the most suitable option for a specific situation.

The gravatar service is not particularly popular, especially in the Russian segment of the internet. Therefore, many comments remain without avatars. For such cases, to ensure that the placeholder avatars are not identical, you can set dynamic avatars (various monsters) in the WordPress settings.

However, sometimes it is necessary to "brand" your site by setting a custom default avatar. This post is specifically for such cases.

Please note that the set default image will only be displayed if the user does not have an avatar. Otherwise, confusion may arise during testing...

Option 1: Adding a Setting for the Default Avatar in the Admin Panel

The code below adds an additional option for the default avatar when selecting it in the admin panel, in the discussion settings.

## Adds another default avatar option to the discussion settings
## The avatar file 'def-avatar.jpg' needs to be uploaded to the theme's 'img' folder
add_filter( 'avatar_defaults', 'add_default_avatar_option' );
function add_default_avatar_option( $avatars ){
	$url = get_stylesheet_directory_uri() . '/img/def-avatar.jpg';
	$avatars[ $url ] = 'Site Avatar';

	return $avatars;
}

As a result, we get:

optsiya-avatarka-po-umolchaniyu

Note: To find out the currently selected default avatar, you can get the value of the option get_option('avatar_default').

The drawback of this method is that you cannot simply set the code, you also need to "go" to the admin panel and enable the default avatar. This approach may be unacceptable when a strict avatar needs to be set that cannot be changed... Or when the specified avatar needs to always be the default, regardless of what is set in the settings.

Especially for such cases, I wrote the second option. It turned out to be not as simple as I expected, but this is unnecessary lyrical digression, so we'll skip it...

Option 2: Setting the Default Avatar "On the Fly" Using a Filter

This option sets the default avatar immediately after inserting the code and does not allow changing the avatar setting in the admin panel.

The hook takes into account the default parameter specified in the avatar output or retrieval functions. For example, if the get_avatar() function is called with the default parameter, the avatar specified in the default parameter will be set, not the one specified in the hook.

## Sets the default avatar
## The avatar file 'def-avatar.jpg' needs to be placed in the theme's 'img' folder
add_filter( 'pre_get_avatar_data', 'replace_default_avatar' );
function replace_default_avatar( $args ){
	// admin and discussion page, where the default avatar is configured...
	// if( is_admin() && get_current_screen()->base === 'options-discussion' ) return $args;

	static $defava;
	if( $defava === null ){
		$defava = get_option( 'avatar_default', 'mystery' ); // let's speed up
	}

	// If the default avatar is used, to make the 'default' parameter work for avatar functions
	// if it is set
	if( ! $args['default'] || $defava == $args['default'] ){
		$args['default'] = get_stylesheet_directory_uri() . '/img/def-avatar.jpg';
	}

	return $args;
}

Option 3: Specifying the Link to the Default Avatar When Calling the Function

This is perhaps the least flexible option. I don't remember ever using it. But it can definitely be useful...

To get the avatar or its data, there are three functions:

  • get_avatar() - Retrieves the <img> html code of the commenter's avatar.
  • get_avatar_data() - Retrieves an array of avatar data.
  • get_avatar_url() - Retrieves the URL of the avatar based on the provided email, ID, or user object.

In each of them, you can specify the default parameter and thus set the URL of the default avatar. I will provide an example for each function, where the default parameter is specified:

echo get_avatar( '[email protected]', 48, 'http://example.com/default-avatar.png' );
$ava_data = get_avatar_data( '[email protected]', array(
	'size' => 48,
	'default' => 'http://example.com/default-avatar.png',
) );

echo $ava_data['url'];
$url = get_avatar_url( '[email protected]', [
	'size' => 48,
	'default' => 'http://example.com/default-avatar.png',
] );

echo $url;

WordPress Avatar Plugins

Finally, here are a few plugins on the topic:

  • Avatar Manager - adds an avatar upload field to the profile, allowing each user to set their own avatar without using the gravatar service. The avatar is displayed using the get_avatar() function.

  • Basic User Avatars - adds the ability to upload avatars in the user profile. It supports front-end uploads and has bbPress support.

  • WP First Letter Avatar - sets the first letter of the author's name as the avatar. It supports Cyrillic, Latin, and numbers.

I have not tried any of these plugins in practice and have not closely examined them, so I cannot comment on their quality, etc...