How to modify the default Gravatar avatar in WordPress

Article directory [hide]

  • Method 1: Use code to customize the default Gravatar avatar of WordPress
  • Method 2: Use Add New Default Avatar plug-in

Yesterday @Cain asked Advocate how to modify the default Gravatar avatar of WordPress, that is to say, if the visitor has his own Gravatar avatar, it will display his own, and if not, it will display the one specified by the website.

Friends who are familiar with WordPress should know that in the background-Settings-discussion, you can set the default Gravatar avatar, but the built-in icons are really not flattering. Fortunately, WordPress provides the avatar_defaults hook, allowing us to add self Define the avatar, and share 2 ways to modify it.

Method 1: Use code to customize the default Gravatar avatar of WordPress

Add the following code before the last ?> in functions.php of your theme:

1
2
3
4
5
6
7
add_filter (  'avatar_defaults' ,  'newgravatar'  ) ;  
 
function newgravatar ( $avatar_defaults )  {   
    $myavatar  = get_bloginfo ( 'template_directory' )  .  '/ images/wpdaxue-gravatar.jpg ' ;   
    $avatar_defaults [ $myavatar ]  =  "WordPress university default avatar" ;   
    return  $avatar_defaults ;   
}

Modification tips:

1. The fourth line of code is the URL address of the avatar, the above is wpdaxue-gravatar.jpg under the images folder of the current theme, please modify it according to the actual situation;

2. The fifth line is the name of the custom avatar. The above is the default avatar of the WordPress university. Please modify it yourself.

After saving, you can see the newly added avatar under the background-Settings-Discussion, just set it as the default:

wpdaxue.com-201303388

Method 2: Use Add New Default Avatar plug-in

If you don’t want to toss the code, you can also install the Add New Default Avatar plug-in directly in the background. After enabling it, you can add a custom avatar under the background-Settings-Discussion, which is very simple.

Leave a Reply

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