模板兔开发的部分主题里会有前端头像上传的功能,那么怎么才能让头像同步后台与前端呢?我们这里去除默认的头像,用固定的某张图片代替,大大加快加载速度。
代码如下:
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 个评论