By

Set a New Default Avatar with Filter Hooks

One way to add a bit of extra uniqueness to your theme is to include a new default avatar. The standard one we all know and love is the ‘Mystery Man’ avatar as seen below.

There is nothing wrong with this avatar but it is handy to be able to include a new avatar with your theme AND set it as the active default avatar that will be used in post/page comments..

It is fairly easy to include a new avatar to the list of standard ones that ship with WordPress but you need an extra bit of code to actively set it as the new default. That is what we will do below.

Firstly let’s see an example avatar that we could include with a new theme. Here is our new mystery man.

We will be adding this new a new avatar to the current list of available ones, not replacing it. They will both be available from the Settings => Discussion admin page. So, to include the new mystery man avatar to the existing list, add the following code to your themes functions.php file:

1
2
3
4
5
6
7
// add a new default avatar to the list in WordPress admin
function mytheme_addgravatar( $avatar_defaults ) {
	$myavatar = get_bloginfo('template_directory') . '/images/icons/new_mystery_man.png';
	$avatar_defaults[$myavatar] = 'New Default Gravatar';
	return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'mytheme_addgravatar' );

This will add a new avatar to the end of the current list. Below is a screenshot of the new avatars available:

However it is still not set as the active default avatar. This is no real issue as the user can always set this manually as part of the theme set up, but it would be easier to do this automatically!

To do this we need to add some code that sets the active default avatar. We need this code to only run once when the theme is activated. Otherwise our new default avatar will be ALWAYS be set even if the user tries to select a different one manually.

Unfortunately there is no theme activation hook in WordPress so have to use a workaround. Thanks to Ozh for the simulated theme activated hook! Add the following code to your themes functions.php file:

1
2
3
4
5
// there is no theme activation hook but we can use this instead to update an option upon theme activation
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
	$myavatar = get_bloginfo('template_directory') . '/images/icons/user.png';
	update_option( 'avatar_default' , $myavatar ); // set the new avatar to be the default 
}

Now when the theme is activated it will update the default avatars with our new mystery man one and set it to be current default avatar automatically!

The full code to do this is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
// there is no theme activation hook but we can use this instead to update an option upon theme activation
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
	$myavatar = get_bloginfo('template_directory') . '/images/icons/user.png';
	update_option( 'avatar_default' , $myavatar ); // set the new avatar to be the default 
}
 
// add a new default avatar to the list in WordPress admin
function mytheme_addgravatar( $avatar_defaults ) {
	$myavatar = get_bloginfo('template_directory') . '/images/icons/user.png';
	$avatar_defaults[$myavatar] = 'New Default Gravatar';
	return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'mytheme_addgravatar' );

Now when we view our posts/page comments we get something like this:

10 Responses to Set a New Default Avatar with Filter Hooks

  1. Remkus says:

    Saw your post on the Tavern and funnily enough I was looking for the same answer. Thanks for sharing it here!

    This blog could do with a nice code highlighter btw :)

    • dgwyer says:

      Ooh, you caught me out. I was just about to install a highlighter plugin! It will be in glorious highlighted syntax in just a moment.. :)

      Glad you found it useful too, I am going to be sharing lots more on here. I know I need to blog more, just have been really busy lately (lame excuse I know).

  2. Pingback: Tweets that mention Set a New Default Avatar with Filter Hooks | Press Coders -- Topsy.com

  3. Pingback: What is the best bluetooth software to download?

  4. IsaacAlbeniz says:

    it was very interesting to read.
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  5. Hendra S says:

    Thanks!
    Still working with WordPress 3.9 😉