wordpress中Gravatar用户头像不显示,免插件实现添加自定义设置上传头像功能

该功能主要是用于免插件为用户添加头像,解决头像无法显示或无法更换自定义头像的问题

虽热这个功能使用场景和频率都非常低,但在有时候还是需要WordPress来显示头像的,但是zuanmang.net并不是每个人都有注册设置Gravatar头像。所以便需要我们手动为WordPress添加后台可自定义上传头像的功能,如下:

将下面的代码加入到你主题的Functions.php 文件中,然后刷新页面。进入用户资料页,滑到底部,点击上传头像即可

//自定义头像class Simple_Local_Avatars {private $user_id_being_edited;public function __construct() {add_filter( 'get_avatar', array( $this, 'get_avatar' ), 10, 5 );add_action( 'admin_init', array( $this, 'admin_init' ) );add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) );add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );add_filter( 'avatar_defaults', array( $this, 'avatar_defaults' ) );}public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = false ) {if ( is_numeric($id_or_email) )$user_id = (int) $id_or_email;elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )$user_id = $user->ID;elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )$user_id = (int) $id_or_email->user_id;if ( empty( $user_id ) )return $avatar;$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );if ( empty( $local_avatars ) || empty( $local_avatars['full'] ) )return $avatar;$size = (int) $size;if ( empty( $alt ) )$alt = get_the_author_meta( 'display_name', $user_id );// generate a new sizeif ( empty( $local_avatars[$size] ) ) {$upload_path = wp_upload_dir();$avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );$image_sized = image_resize( $avatar_full_path, $size, $size, true );      // deal with original being >= to original image (or lack of sizing ability)$local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );// save updated avatar sizesupdate_user_meta( $user_id, 'simple_local_avatar', $local_avatars );} elseif ( substr( $local_avatars[$size], 0, 4 ) != 'http' ) {$local_avatars[$size] = home_url( $local_avatars[$size] );}$author_class = is_author( $user_id ) ? ' current-author' : '' ;$avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";return apply_filters( 'simple_local_avatar', $avatar );}public function admin_init() {//load_plugin_textdomain( 'simple-local-avatars', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );register_setting( 'discussion', 'simple_local_avatars_caps', array( $this, 'sanitize_options' ) );add_settings_field( 'simple-local-avatars-caps', __('Local Avatar Permissions','simple-local-avatars'), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );}public function sanitize_options( $input ) {$new_input['simple_local_avatars_caps'] = empty( $input['simple_local_avatars_caps'] ) ? 0 : 1;return $new_input;}public function avatar_settings_field( $args ) {       $options = get_option('simple_local_avatars_caps');echo '<label for="simple_local_avatars_caps"><input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked( $options['simple_local_avatars_caps'], 1, false ) . ' />' . __('仅具有头像上传权限的用户具有设置本地头像权限(作者及更高等级角色)。','simple-local-avatars') . '</label>';}public function edit_user_profile( $profileuser ) {?><h3><?php _e( '头像','simple-local-avatars' ); ?></h3><table class="form-table"><tr><th><label for="simple-local-avatar"><?php _e('上传头像','simple-local-avatars'); ?></label></th><td style="width: 50px;" valign="top"><?php echo get_avatar( $profileuser->ID ); ?></td><td><?php$options = get_option('simple_local_avatars_caps');if ( empty($options['simple_local_avatars_caps']) || current_user_can('upload_files') ) {do_action( 'simple_local_avatar_notices' );wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );?><input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br /><?phpif ( empty( $profileuser->simple_local_avatar ) )echo '<span class="description">' . __('尚未设置本地头像,请点击“浏览”按钮上传本地头像。','simple-local-avatars') . '</span>';elseecho '<input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('移除本地头像','simple-local-avatars') . '<br /><span class="description">' . __('如需要修改本地头像,请重新上传新头像。如需要移除本地头像,请选中上方的“移除本地头像”复选框并更新个人资料即可。<br/>移除本地头像后,将恢复使用 Gravatar 头像。','simple-local-avatars') . '</span>';     } else {if ( empty( $profileuser->simple_local_avatar ) )echo '<span class="description">' . __('尚未设置本地头像,请在 Gravatar.com 网站设置头像。','simple-local-avatars') . '</span>';elseecho '<span class="description">' . __('你没有头像上传权限,如需要修改本地头像,请联系站点管理员。','simple-local-avatars') . '</span>';}?></td></tr></table><script type="text/javascript">var form = document.getElementById('your-profile');form.encoding = 'multipart/form-data';form.setAttribute('enctype', 'multipart/form-data');</script><?php       }public function edit_user_profile_update( $user_id ) {if ( ! isset( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) )            //securityreturn;if ( ! empty( $_FILES['simple-local-avatar']['name'] ) ) {$mimes = array('jpg|jpeg|jpe' => 'image/jpeg','gif' => 'image/gif','png' => 'image/png','bmp' => 'image/bmp','tif|tiff' => 'image/tiff');// front end (theme my profile etc) supportif ( ! function_exists( 'wp_handle_upload' ) )require_once( ABSPATH . 'wp-admin/includes/file.php' );$this->avatar_delete( $user_id );    // delete old images if successful// need to be more secure since low privelege users can uploadif ( strstr( $_FILES['simple-local-avatar']['name'], '.php' ) )wp_die('For security reasons, the extension ".php" cannot be in your file name.');$this->user_id_being_edited = $user_id; // make user_id known to unique_filename_callback function$avatar = wp_handle_upload( $_FILES['simple-local-avatar'], array( 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array( $this, 'unique_filename_callback' ) ) );if ( empty($avatar['file']) ) {     // handle failuresswitch ( $avatar['error'] ) {case 'File type does not meet security guidelines. Try another.' :add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("请上传有效的图片文件。","simple-local-avatars"));') );              break;default :add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error","<strong>".__("上传头像过程中出现以下错误:","simple-local-avatars")."</strong> ' . esc_attr( $avatar['error'] ) . '");') );}return;}update_user_meta( $user_id, 'simple_local_avatar', array( 'full' => $avatar['url'] ) );      // save user information (overwriting old)} elseif ( ! empty( $_POST['simple-local-avatar-erase'] ) ) {$this->avatar_delete( $user_id );}}/*** remove the custom get_avatar hook for the default avatar list output on options-discussion.php*/public function avatar_defaults( $avatar_defaults ) {remove_action( 'get_avatar', array( $this, 'get_avatar' ) );return $avatar_defaults;}/*** delete avatars based on user_id*/public function avatar_delete( $user_id ) {$old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );$upload_path = wp_upload_dir();if ( is_array($old_avatars) ) {foreach ($old_avatars as $old_avatar ) {$old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );@unlink( $old_avatar_path );   }}delete_user_meta( $user_id, 'simple_local_avatar' );}public function unique_filename_callback( $dir, $name, $ext ) {$user = get_user_by( 'id', (int) $this->user_id_being_edited );$name = $base_name = sanitize_file_name( substr(md5($user->user_login),0,12) . '_avatar' );$number = 1;while ( file_exists( $dir . "/$name$ext" ) ) {$name = $base_name . '_' . $number;$number++;}return $name . $ext;}
}$simple_local_avatars = new Simple_Local_Avatars;function get_simple_local_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {global $simple_local_avatars;$avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );if ( empty ( $avatar ) )$avatar = get_avatar( $id_or_email, $size, $default, $alt );return $avatar;
}
//自定义头像 结束

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/887452.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

aws服务--机密数据存储KMS(1)介绍和使用

在AWS(Amazon Web Services)中存储机密数据时,安全性和合规性是最重要的考虑因素。AWS 提供了多个服务和工具,帮助用户确保数据的安全性、机密性以及合规性。AWS Secrets Manager、KMS(Key Management Service)是推荐的存储机密数据的AWS服务和最佳实践。这里先看KMS。 …

51c~C语言~合集2

我自己的原文哦~ https://blog.51cto.com/whaosoft/12652943 一、嵌入式开发中的C语言编译器 如果你和一个优秀的程序员共事&#xff0c;你会发现他对他使用的工具非常熟悉&#xff0c;就像一个画家了解他的画具一样。----比尔.盖茨1 不能简单的认为是个工具 嵌入式程序开发…

ensp静态路由实验

一、实验目的 1、熟练掌握交换机的基本配置命令 2、熟练掌握静态路由的使用方法 3. 熟练掌握交换机端口模式 二、实验内容 需求&#xff1a; 根据要求利用现有实验设备组建小型局域网 实验设备&#xff1a; 交换机S37002台&#xff1b;PC机2台&#xff1b;路由器2台。 …

【git】commit之后,想撤销commit

一、已经commit&#xff0c;想要回退到上一步 保留代码 git reset --soft HEAD^回退到具体的哪一步 HEAD^的意思是上一个版本&#xff0c;也可以写成HEAD~1如果你进行了2次commit&#xff0c;想都撤回&#xff0c;可以使用HEAD~2二、git reflog 查看 sha值 git reflog 回到…

在 Ubuntu 上安装 Yarn 环境

在 Ubuntu 上安装 Yarn 环境 步骤 1: 更新系统步骤 2: 安装 Node.js步骤 3: 安装 Yarn方法 1: 使用 npm 安装方法 2: 使用 APT 安装 步骤 4: 验证安装总结 在 Ubuntu 上安装 Yarn 环境可以通过以下步骤完成&#xff1a; 步骤 1: 更新系统 首先&#xff0c;确保你的系统是最新…

深度学习3

五、自动微分 1、基础概念 模块 autograd 负责自动计算张量操作的梯度&#xff0c;具有自动求导功能&#xff1b;autograd 创建一个动态计算图来跟踪张量的操作&#xff0c;每个张量是计算图中的一个节点&#xff0c;节点之间的操作构成图的边。 属性 requires_grad 决定…

PostgreSQL 中约束Constraints

在 PostgreSQL 中&#xff0c;约束&#xff08;Constraints&#xff09;是用于限制进入数据库表中数据的规则。它们确保数据的准确性和可靠性&#xff0c;通过定义规则来防止无效数据的插入或更新。PostgreSQL 支持多种类型的约束&#xff0c;每种约束都有特定的用途和语法。以…

路由器中继与桥接

一 . 背景 现在的路由器大多数已经开始支持多种网络连接模式&#xff0c;以下将以TP-Link迷你无线路由器为例进行展开介绍。在TP-Link迷你无线路由器上一般有AP&#xff08;接入点&#xff09;模式&#xff0c;Router&#xff08;无线路由&#xff09;模式&#xff0c;Repeate…

2.13 转换矩阵

转换矩阵引用了库nalgebra&#xff0c;使用时研究具体实现。 use std::ops;use nalgebra::Perspective3;use crate::Scalar;use super::{Aabb, LineSegment, Point, Triangle, Vector};/// An affine transform #[repr(C)] #[derive(Debug, Clone, Copy, Default)] pub struct…

SQL进阶:如何跳过多个NULL值取第一个非NULL值?

NULL 一、问题描述二、ORACLE<一>、last_value () over ()<二>、lag () over()<三>、相关子查询 三、MYSQL<一>、全局变量<二>、coalesce() lag() over()<三>、相关子查询<四>、 recursive<五>、lag() over() min() over() …

wordpress获取文章总数、分类总数、tag总数等

在制作wordpress模板的时候会要调用网站的文章总数分类总数tag总数等这个数值&#xff0c;如果直接用count查询数据库那就太过分了。好在wordpress内置了一些标签可以直接获取到这些数值&#xff0c;本文整理了一些常用的wordpress网站总数标签。 文章总数 <?php $count_…

人工智能|计算机视觉——微表情识别(Micro expression recognition)的研究现状

一、简述 微表情是一种特殊的面部表情,与普通的表情相比,微表情主要有以下特点: 持续时间短,通常只有1/25s~1/3s;动作强度低,难以察觉;在无意识状态下产生,通常难以掩饰或伪装;对微表情的分析通常需要在视频中,而普通表情在图像中就可以分析。由于微表情在无意识状态…

玩转合宙Luat教程 基础篇④——程序基础(库、线程、定时器和订阅/发布)

文章目录 一、前言二、库三、线程四、定时器五、订阅/发布5.1 回调函数5.2 堵塞等待一、前言 教程目录大纲请查阅:玩转合宙Luat教程——导读 写一写Lua程序基础的东西。 包括如何调用库,如何创建线程、如何创建定时器,如何使用订阅/发布事件。 二、库 程序从main.lua开始通…

嵌入式系统与OpenCV

目录 一、OpenCV 简介 二、嵌入式 OpenCV 的安装方法 1. Ubuntu 系统下的安装 2. 嵌入式 ARM 系统中的安装 3. Windows10 和树莓派系统下的安装 三、嵌入式 OpenCV 的性能优化 1. 介绍嵌入式平台上对 OpenCV 进行优化的必要性。 2. 利用嵌入式开发工具&#xff0c;如优…

CentOS环境上离线安装python3及相关包

0. 准备操作系统及安装包 准备操作系统环境&#xff1a; 首先安装依赖包&#xff0c;安装相应的编译工具 [rootbigdatahost bin]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-d…

在 Ubuntu 上安装 MinIO 并设置密码

在 Ubuntu 上安装 MinIO 并设置密码 1. 更新系统2. 设置时区为中国大陆3. 安装 MinIO3.1 下载 MinIO3.2 赋予执行权限3.3 移动 MinIO 到系统路径 4. 创建 MinIO 用户和数据目录4.1 创建用户4.2 创建数据目录4.3 设置权限 5. 配置 MinIO5.1 创建配置文件 6. 创建 Systemd 服务文…

数学建模_基于对数和傅里叶变换的多通道图像增强模型(处理模糊)Matlab代码包教会使用,直接替换数据即可

图像增强模型&#xff1a;基于对数和傅里叶变换的多通道增强 模型简介 本博客介绍一种基于对数变换&#xff08;Logarithmic Transformation&#xff09;和傅里叶变换&#xff08;FFT&#xff09;的图像增强方法。该方法结合多尺度高斯滤波器和拉普拉斯模糊度分布评估&#xf…

Qt交叉编译x86和arm心得

最近一直在Linux上开发qt程序&#xff0c;主要工作是在x86的Ubuntu上开发编译调试程序&#xff0c;确定没有问题后交叉编译到arm的linux系统上运行 1.环境 Qt的交叉编译环境厂家已经提供了&#xff0c;嵌入式的同事帮我安装调试的&#xff0c;具体就是装了厂家给的gcc编译套件…

LeetCode739. 每日温度(2024冬季每日一题 15)

给定一个整数数组 temperatures &#xff0c;表示每天的温度&#xff0c;返回一个数组 answer &#xff0c;其中 answer[i] 是指对于第 i 天&#xff0c;下一个更高温度出现在几天后。如果气温在这之后都不会升高&#xff0c;请在该位置用 0 来代替。 示例 1: 输入: temperatu…

React(五)——useContecxt/Reducer/useCallback/useRef/React.memo/useMemo

文章目录 项目地址十六、useContecxt十七、useReducer十八、React.memo以及产生的问题18.1组件嵌套的渲染规律18.2 React.memo18.3 引出问题 十九、useCallback和useMemo19.1 useCallback对函数进行缓存19.2 useMemo19.2.1 基本的使用19.2.2 缓存属性数据 19.2.3 对于更新的理解…