How to filter get_avatar?

I want to use local avatar system for my wp website , I want to filter the get_avatar() function so it will return the avatar from my server instead of the default gavatar .

I have the following code :

Read More
class local_avatar{
        
        function __construct(){
            add_filter('get_avatar',array($this,'get_avatar'));
        }
        
        public function get_avatar($avatar , $id_or_email , $size = '96'  , $default , $alt = false){
            
            global $wpdb;
            $upload = wp_upload_dir();
            
            if(is_numeric($id_or_email)){
                // the user id
                $image = get_user_meta($id_or_email,'_ahaali_avatar',true);
                if($image){
                    $image_path = $upload['baseurl'].'/avatar/'.$image;
                    $ahaali_avatar = '<img src="'.$image_path.'" width="'.$size.'" height="'.$size.'" />';
                }else{
                    $image_path = 'http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s='.$size;
                    $ahaali_avatar = '<img src="'.$image_path.'" width="'.$size.'" height="'.$size.'" />';
                }
            }elseif(is_string($id_or_email)){
                // the user email
                $user_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM wp_users WHERE user_email = %s",
                $id_or_email));
                $image = get_user_meta($user_id,'_ahaali_avatar',true);
                if($image){
                    $image_path = $upload['baseurl'].'/avatar/'.$image;
                    $ahaali_avatar = '<img src="'.$image_path.'" width="'.$size.'" height="'.$size.'" />';
                }else{
                    $image_path = 'http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s='.$size;
                    $ahaali_avatar = '<img src="'.$image_path.'" width="'.$size.'" height="'.$size.'" />';
                }
            }
            return $ahaali_avatar;
        }
        
    }

And it returns the following errors :

Warning: Missing argument 2 for local_avatar::get_avatar() in /home/subhi/public_html/work/example.com/wp-content/plugins/ahaali_avatar/core.php on line 40

Warning: Missing argument 4 for local_avatar::get_avatar() in /home/subhi/public_html/work/example.com/wp-content/plugins/ahaali_avatar/core.php on line 40

How can I solve this?

Related posts

Leave a Reply

2 comments

  1. The Original Poster was missing the pair priority, parameters when declaring the filter hook:

    add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );
    

    Being 5 all the parameters the callback function can use:

    public function get_avatar( $avatar, $id_or_email, $size, $default, $alt )
    {
        return $avatar;
    }
    
  2. You can easy add new avatars with this function:

     // Custom Default Avatar
     if ( !function_exists('addgravatar') ) {
       function addgravatar( $avatar_defaults ) {
         $myavatar = get_bloginfo('template_directory').'/images/custom-gravatar.jpg'; 
    
         // Change path to your custom avatar
         $avatar_defaults[$myavatar] = 'Custom Avatar'; 
    
         // Change to your avatar name
            return $avatar_defaults;
       }
       add_filter( 'avatar_defaults', 'addgravatar' );
     }
    

    In the admin area go to the Settings tabs –> Discussion and select your new avatar as default and click save.