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;
}

Modia 自媒体 wordpress主题

1 条评论

  1. 有没有代码实现,后台用户可以自定义头像?
    维棠尊2016-09-09
定制
本人承接WordPress等系统建站仿站、二次开发、主题插件定制等
在线咨询
  • 请直接说明需求,勿问在否

联系站长