Inicio > English > WordPress 3.4 and higher Custom Avatars the proper way

WordPress 3.4 and higher Custom Avatars the proper way

Domingo, 27 de enero de 2013

There is a new version of this that works with 3.51 read it here:  WordPress 3.51 the proper way to get custom avatars
There are about 1 million results in google to implement avatars, local avatars, remove gravatars, some even explain how to turn off all avatars from the back-end.

I had this project where i needed to:

  • Show Avatars
  • Remove calls to
  • Use local default Avatar
  • Add custom avatar for the blog users
  • Display Custom avatar for blog users
  • Display default avatar for unregistered commenter
  • Display custom avatar for comment replies

Buy me a beer

Digging for 3 days around google got me two posts which gave me the initial code:

Add default Custom Avatar:

Custom Avatar Field by Bill Erickson:

However the first one only allows to define a custom local default avatar. (Still calls and if nothing found displays a local image)
The second one only works properly on pages, while on comments shows the avatar of the author of the page for unregistered commenter.

During a few days struggling with code examples that did not completely solve my problem I decided to develop a solution myslef.
I dug into wp-includes/pluggable.php and after a struggle understood how WordPress was calling the avatars and what needed doing to get the results I wanted.

No core files should be modified and that was the hardest part.

So I came up with a nice modification in functions.php which i decided to convert in a plugin for the less savvy users.

To use it:

  • Download the file
  • extract the ZIP (1 folder: proper-avatars containing 2 files readme.txt and proper-avatars.php)
  • copy the folder to your /wordpress/wp-content/plugins folder
  • go to the admin area and start defining avatars for your users (the field is added at the end of user profiles)

Alternatively: copy the code at the end of the article in your functions.php file in your theme folder

The demo is available  on the main site:

<!--?php /* Plugin Name: Proper Custom Avatars Plugin URI: Description: Proper way of displaying Custom avatars, remove calls to, Use local default Avatar, Add custom avatar for the blog users,Display Custom avatar for blog users, Display default avatar for unregistered commenter, Display custom avatar for comment replies. Version: 0.1.0 License: GPL Author: Nathaniel Narvaez ( Author URI: */ /*===================================================================================  * Add default Custom Avatar   * @  * =================================================================================*/ add_filter( 'avatar_defaults', 'newgravatar' ); function newgravatar ($avatar_defaults) { $myavatar = get_bloginfo('stylesheet_directory').'/imagenes/escucha.jpg';		// Change this to point to where your custom 												// avatar is stored $avatar_defaults[$myavatar] = "Avatar de Rockdio"; 					// Change this to describe your Default Avatar return $avatar_defaults; }  /*===================================================================================  * Use Custom Avatar if Provided  * If no Avatar is found the default from the function above is used  * =================================================================================*/ function be_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) { 	if (!is_object($id_or_email)) { 												//Returns the Local Avatar for Posts and Pages 			$custom_avatar = get_the_author_meta('be_custom_avatar'); 			if(!empty($custom_avatar)) 			$avatarfound = $custom_avatar; 			elseif(empty($custom_avatar)){ 												//Returns the Local Avatar for ADMIN AREA  				$id = $id_or_email; 				$custom_avatar = get_user_meta($id,'avatar',true);   				if(!empty($custom_avatar)){ 				$avatarfound = $custom_avatar; 				} 			} 		} 	elseif ( !empty($id_or_email--->user_id) ) {
												//Returns the Local Avatar for Comments
			$id = (int) $id_or_email->user_id;
			$user = get_userdata($id);
			if ( $user){
												//Returns the Local Avatar for comments if User has one
				$email = $user->user_email;
				$custom_avatar = get_user_meta($user,'be_custom_avatar',true);
				$avatarfound = $custom_avatar;
	elseif ( !empty($id_or_email->comment_author_email) ) {
												//Returns the DEFAULT Local Avatar for comments
			$email = $id_or_email->comment_author_email;
			$avatarfound = $default;

	if ($avatarfound =='')
			$avatarfound = $default;

	$avatar = "</pre>
<img class="avatar avatar-{$size} photo" src="{$avatarfound}" alt="{$safe_alt}" width="{$size}" height="{$size}" />
	return $avatar;

add_filter('get_avatar', 'be_gravatar_filter', 1, 5);

 * Add Custom Avatar Field @param object $user
 * @link
 * =================================================================================*/
function be_custom_avatar_field( $user ) { ?></pre>
<h3>Custom Avatar</h3>
<th><label for="be_custom_avatar">Custom Avatar URL:</label></th>
<input id="be_custom_avatar" style="width: 400px;" type="text" name="be_custom_avatar" value="<?php echo esc_attr( get_the_author_meta( 'be_custom_avatar', $user->ID ) ); ?>" />

 <span>Type in the URL of the image you'd like to use as your avatar. This will override your default Gravatar, or show up if you don't have a Gravatar.
<strong>Image should be 70x70 pixels.</strong></span></td>
<pre>	<!--?php  } add_action( 'show_user_profile', 'be_custom_avatar_field' ); add_action( 'edit_user_profile', 'be_custom_avatar_field' );   /*===================================================================================  * Save Custom Avatar Field @param int $user_id @author Bill Erickson  * @link  * =================================================================================*/ function be_save_custom_avatar_field( $user_id ) { 	if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } 		update_usermeta( $user_id, 'be_custom_avatar', $_POST['be_custom_avatar'] ); } add_action( 'personal_options_update', 'be_save_custom_avatar_field' ); add_action( 'edit_user_profile_update', 'be_save_custom_avatar_field' ); ?-->

Author: Categories: English Tags:
  1. Sin comentarios aún.
  1. Sin trackbacks aún.