wordpress收费下载资源主题
wordpress付费下载资源插件

wordpress 自定义用户头像同步前端后台

模板兔开发的部分主题里会有前端头像上传的功能,那么怎么才能让头像同步后台与前端呢?我们这里去除默认的头像,用固定的某张图片代替,大大加快加载速度。

代码如下:

add_filter('get_avatar', 'MBT_get_avatar', 10, 3);
function MBT_get_avatar($avatar, $id_or_email, $size){
 $default_avatar = get_bloginfo('template_url').'/img/avatar.png'; //默认头像
 if(is_object($id_or_email)) {
 if($id_or_email->user_id != 0) {
 $email = $id_or_email->user_id;
 $user = get_user_by('email',$email);
 $user_avatar = get_user_meta($id_or_email->user_id, 'avatar', true);
 if($user_avatar)
 return '<img src="'.$user_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 else
 return '<img src="'.$default_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 
 }elseif(!empty($id_or_email->comment_author_email)) {
 //$user = get_user_by('email', $id_or_email->comment_author_email);
 //$email = !empty($user) ? $user->ID : $id_or_email->comment_author_email;
 return '<img src="'.$default_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 }
 }else{
 if(is_numeric($id_or_email) && $id_or_email > 0){
 $user = get_user_by('id',$id_or_email);
 $user_avatar = get_user_meta($id_or_email, 'avatar', true);
 if($user_avatar)
 return '<img src="'.$user_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 else
 return '<img src="'.$default_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 }elseif(is_email($id_or_email)){
 $user = get_user_by('email',$id_or_email);
 $user_avatar = get_user_meta($user->ID, 'avatar', true);
 if($user_avatar)
 return '<img src="'.$user_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 else
 return '<img src="'.$default_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="'.$user->display_name .'" />';
 }else{
 return '<img src="'.$default_avatar.'" class="avatar avatar-'.$size.' photo" width="'.$size.'" height="'.$size.'" alt="" />';
 }
 }
 return $avatar;
}

2 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请先加Q,临时会话收不到
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情