Old code in my plugin replaced the get_avatar. It stopped working with the release of WP 4.4, therefore, I decided to try to learn how to use add_filter instead.
There are other posts discussing the number of arguments / parameters being passed, etc. I've even looked at WordPress plugins in the repository which use add_filter, however, even a stripped down file is only loading the $avatar_defaults rather than the add_filter location.
Here is my "stripped down" version to test how to filter get_avatar. You'll see the method set_avatar should try to load a test.png but instead is loading the xenforo_logo.og.png for any non-Gravatar account.
class XenWord_Avatars {
public function construct() {
$this->hooks();
}
public function hooks() {
add_filter( 'get_avatar', array( $this, 'set_avatar' ), 10, 5 );
}
public function set_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
if ( is_admin() ) {
$screen = get_current_screen();
if ( is_object($screen) && in_array( $screen->id, array( 'dashboard', 'edit-comments' ) ) && $default == 'mm') {
$default = get_option( 'avatar_default', 'mystery' );
}
}
if ( $default != 'XenForo' ) {
return $avatar;
}
if ( $default == 'XenForo' ) {
$newAvatar = plugins_url( '../admin/images/test.png', __FILE__ );
return $newAvatar;
}
return $avatar;
}
}
$xenword_avatars = new XenWord_Avatars();
/**
* Add Custom Avatar (Discussion Settings)
*
* @param $avatar_defaults
*
* @return mixed
* @throws Zend_Exception
*/
function xenword_avatar_defaults( $avatar_defaults ) {
// Get Avatar from plugin folder
$new_avatar = plugins_url( '../admin/images/xenforo_logo.og.png', __FILE__ );
$avatar_defaults[$new_avatar] = "XenForo";
// Remove default avatars
//unset ( $avatar_defaults['mystery'] );
//unset ( $avatar_defaults['blank'] );
//unset ( $avatar_defaults['gravatar_default'] );
//unset ( $avatar_defaults['identicon'] );
//unset ( $avatar_defaults['wavatar'] );
//unset ( $avatar_defaults['monsterid'] );
//unset ( $avatar_defaults['retro'] );
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'xenword_avatar_defaults' );
Can anyone see anything obviously wrong with what is done in the above code? Once I figure out what is wrong then I can extend this method so that it gets the avatar from XenForo based on the user account.