How to filter get_avatar?

时间:2012-06-27 作者:Paul Dumitru

我想为我的wp网站使用本地头像系统,我想过滤get_avatar() 它将从我的服务器返回头像,而不是默认的gavatar。

我有以下代码:

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;
        }
        
    }
并返回以下错误:

警告:在/home/subhi/public\\u html/work/example中缺少local\\u avatar::get\\u avatar()的参数2。com/wp-content/plugins/ahaali\\u-avatar/core。php在线40

警告:在/home/subhi/public\\u html/work/example中缺少local\\u avatar::get\\u avatar()的参数4。com/wp-content/plugins/ahaali\\u-avatar/core。php在线40

我如何解决这个问题?

2 个回复
SO网友:brasofilo

原来的海报找不到这对priority, parameters 声明筛选器挂钩时:

add_filter( \'get_avatar\', array( $this, \'get_avatar\' ), 10, 5 );
存在5 回调函数可以使用的所有参数:

public function get_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
    return $avatar;
}

SO网友:Pontus Abrahamsson

您可以使用此功能轻松添加新头像:

 // 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\' );
 }
在管理区域中,转到设置选项卡–>讨论,选择新头像作为默认头像,然后单击保存。

结束

相关推荐

Run shortcode before filters

我的用户在注释中发布代码片段。我为此创建了一个快捷码:function post_codigo($atts,$content=\"\"){ return \'<code>\'.$content.\'</code>\'; } add_shortcode(\'codigo\',\'post_codigo\'); 问题是html在打包到代码标记之前会被过滤掉。我想如果我能在过滤器之前运行短代码,那么我可以使用fun