How to Add Avatar Defaults in WordPress

by Vincy. Last modified on July 12th, 2022.

Avatar is used to represent a person by using an icon which will generally have a profile photo image. It is catchy to use images instead of names and also images register easily that a textual name.

WordPress supports the avatar images to represent the author of a post or the users who add comments. There are more options to select the default avatar in WordPress admin.

By default, the “Mystery Person” avatar option is selected and used to iconify the users. WordPress allows us to add our own images to these default avatar options.

In this tutorial, we are going to see how to add new options to the avatar defaults without using any WordPress plugins. I use WordPress filter hook in functions.php to add new options to the avatar defaults.

We can also use plugins to hook a function to add avatar options. In this function, I have added the URL and the title of the avatar image that I want to be selected as the default avatar. In a previous tutorial, we have seen how to add an action hook to register a custom widget.

The following screenshot shows the avatar defaults with the new one I have hooked in the functions.php captioned as “Vincy Avatar”.

add-new-avatar-default

Filter Hook to Add New Avatar Default in WordPress

The following code shows how to use a WordPress filter hook to add a new option to the avatar default settings. In this code, I have passed the avatar image URL which is stored in my themes image folder.

Also, I have specified the caption to display in the avatar settings in WordPress admin.

<?php
add_filter( 'avatar_defaults', 'addNewAvatar' );
function addNewAvatar($avatar_defaults) {
	$myavatar = get_template_directory_uri() . '/images/Vincy.jpg';
	$avatar_defaults[$myavatar] = "Vincy Avatar";
	return $avatar_defaults;
}
?>

WordPress Admin Avatar Settings

After adding the above code in the functions.php, login to the WordPress admin area and navigate via Settings > Discussion and enable show avatar check box to list default avatar options.

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page