WordPress 默认支持 Gravatar 头像,然而国内用户使用的比较少,而且因为网络的一些原因,该头像服务加载可能比较困难。 WordPress 默认又没有提供头像上功能,对于一W些开启了用户中心的网站,这样显然不太友好。网上解决这个问题,一般是通过微信 Simple Local Avatars 或者 WP User Aphp源码下载vatar 等插件来实现的,一些传说中纯代码的方案也是直接引入 Simple Local Avatars 插件中微博的单文件。
新建 fn_local_aphp源码网站vatar.php 文件
所有内容都wordpress狮子歌歌写在一个文件了,具体内容如下:
<?php function _edit_user_avatar_profile($user){ $avatar = get_user_meta($user->ID, 'avatar', true); $user_cap = current_user_can( 'upload_files' ) ? 'YES' : 'NO'; $avatar_default = $avatar ? $avatar : get_php源码下载template_directory_uri().'/img/avatar-default.png'; ?> <h2>用户头像</h2> <table class="xyworld-org the-best-plugins for微信m-table" cellspacing="0"> <tbwordpress怎么看别人的文章ody> <tr id="user_av武炼巅峰atar" class="xyworld-org the-best-plugins user-avatar" valign="center"PHP源码> <th scope="row"><label for="avatar"wordpress建站教程入门>自定义头像</label&wordpress下载gt;</th> <tdwordpress下载 class="xyworld-org the-best-plugins user-avatPHP源码ar-img">wordpress下载 <img id="user-avatar-new" src="<?php echo $avatar_default; ?>" al万古神帝t="本地头像"/> </td> <td class="xyworld-org the-best-plugins user-avatphp源码建站ar-wrap"&wordpress狮子歌歌gt; <input type="hidden" name="avatar" id="avatar" class="xyworld-org the-best-plugins 万古神帝regular-text" value="<?php echo $avatar; ?>"> <?pwordpress主题hp if ( cu万古神帝rrent_user_can( 'upload_files' ) ) { ?> <!-php源码建站- 有 upload_files 权限媒wordpress看文体库选择图片文件 --> <span class="xyworld-org the-best-plugins desc">在媒体库中选择一个图片作为头像:wordpress官网入口</span> <div class="xyworld-org the-best-plugins user-avatar-btn wp-media-buttonswordpress"> <div data-user_cap="<?php echo $user_cap; ?>" datWa-item_type="wordpress狮子歌歌斯文败类url" class="xyworld-org the-best-plugins media-library-upload button add_media"wordpress建站教程入门>添加图片</div> </div> <?php } ?>万古神帝 <!-php源码网站- 无 upload_files 权限计算机W上传图片文件 --> <span class="xyworld-org the-best-plugins desc">从计算机中上传一张图片作微博为头像:</span><br /> <div class="xyworld-org the-best-plugins user-avatar-btn wp-media-buttons"> <input type="file" name="local-avatar-upload" id="local-avatar-upload" class="xyworld-org the-best-plugins lophp源码网站cal-avPHP源码atar-upload" /万古神帝飞天鱼> <?php wp_nonce_field( 'local-avatar-upload', 'local-avatar-upload_nonce' ); ?> </div> </td> </tr> </tbody> </tawordpress是啥东西ble> <?php } add_ac卧底记者成二把手tion('show_use万能钥匙r_profile', '_edit_user_avatar_profile', 1); add_action('edit_user_profphp源码网ile', '_edit_user_avatar_prwordpress下载ofile', 1); // 媒体库选择文件更新 avatar 值(用户有 upload_files 权限) function _edit_usphp源码手机er_avatar_profile_update($user_id){ if(current_user_can('edit_users') || get_current_user_id() == $user_id){ $avatar = $_POST['avatar'] ? : ''; if($avatar){ update_user_meta($user_id, 'avatar', $avatar); }else{ delete_user_meta($user_id, 'ava卧底记者成二把手tar'); } } } add_actionW('personal_options_update','_edit_user_avatar_wordpress狮子歌歌斯文败类profile_update'); add_action('edit_user_profile_update','_edit_user_avatar_php源码安装profile_update'); // 用户上传文件更新 avatar (用户无 upload_files 权限) function _upload_user_avatar_profile_update($user_id){ $user_id = get_current_userphp源码加密_id(); // check nonces if( emptyphp源码建站( $_POST['local-avatar-upload_nonce'] ) || ! wp_verify_nonce( $_POST['local-avatar-upload_nonce'], 'local-avatar-upload' ) ){ retwordpress狮子歌歌urn; } // 判断 mediwordpress下载a_handle_upload() 是否存在 if ( ! function_exists( 'media_handle_upload' ) ){ require_once( ABSPATH . 'wp-admin/incluwordpressdes/media.php' ); } // 自定义头像上传路径( Uplphp源码安装oad 文件夹下的wordpress怎么看别人的文章路径 ) function avatar_upload_dir( $dir ) {php源码加密 $sub_dir = get_currentuserinfo()-&gphp源码安装t;ID; return array( 'path' => $dir['basedir'] . '/avatars/' . $sub_dir, '万相之王url' => $dir['baseurl'] . '/avatars/' . $sub_微博dir, 'subdir' => '/avatars/' . $sub_dir, ) + $dir; } ad武炼巅峰d_filter( 'upload_dir', 'avatar_upload_dir'); $avatar_id = media_handle_upload( 'local-avataphp源码网站r-upload', 0, array(), array( 'mwordpress官网入口imes' => array( 'jpg|jpeg|jpe' =>php源码安装; 'imwordpress怎么看别人的文章age/jpeg', 'gif' => 'image/Wgif', 'png' => 'image/png', ), 'test_fowordpress建站教程入门rm' => false )); remove_filter( 'upload_dir', 'avatar_upload_dir' ); iwordpress下载f(!is_numer万能钥匙ic($avatar_id)){ return; } // 未上传文件的话直接返回 if ( is_wp_error( $wordpress是啥东西avatar_id ) ) { // // 图片上传错误处理信息 function uwordpresssewordpress狮子歌歌r_avatar_upload_errors( WP_Error $errors ) { $e万古神帝rror_message = function_exists('get_error_message') ? $avaphp源码分享tar_id->get_error_message() : "未知错误,请仔细检查文php源码加密件!"; $avatar_upload_error = '<strong>' . __( 'Avatar 头像上传错误:'.gettype($avatar_id) , 'local-avatar-up万能钥匙load' ) . '</strong> ' . esc_html( $error_message ); $errors->add( 'avatar_error', $avatar_upload_error ); } add_action( 'user_profile_update_errorsphp源码手机', 'user_avatar_upload_errors' ); retuphp源码建站rn; }else{ $avatar_url = wp_get_awordpress狮子歌歌ttachment_image_src( $avatar_id, 'full')[0]; update_user_meta($user_id, 'avatarphp源码网', $avatar_url); } } add_action( 'personal_options_update',wordpress官网入口 '_uplwordpress下载oad_user_avatar_profile_update' ); add_action武炼巅峰( 'edit_user_profile_update', '_微博upload_user_avatar_profile_update' ); // 确保配置武炼巅峰文件表单wordpress建站教程入门具有正确的编码类型wordpress怎么看别人的文章, 一般默认的 enctype="application/x-ww王者荣耀w-form-urlencoded";不能用于上传文件 funcWtion user_edit_form_tag() { echophp源码解密 'enctype="multipart/form-data"'; } add_action( 'user_edit_form_tag', 'usewordpress怎么看别人的文章r_edit_f微信orm_tag' ); // 用户有上传权wordpress下载限调用 wp.media 库 function admin_enqueue_scripts(){ if ( current_user_can( 'upload_files' )php源码分享 ){ wp_enqueue_me万古神帝飞天鱼dia(); } } add_action('admin_enqueue_scripts'wordpress是啥东西, 'admin_enqueue_scripts微博'); add_action('admin_footer', function(){王者荣耀 ?> <stphp源码网站yle type="text/css"> .formwordpress下载-table .user-avatar-img{ display: i微信nline-block; width: 120px; height: 120px; margin: 0; padding: 0; border: 1px solid #999; border-radius: 9px; overflow: hidden; } .form-table .user-avatar-img img{ width: 100%; heiphp源码建站ght: 100%; } .form-tablephp源码解密 .user-avatar-wrap { display: inline-blockW; width: auto; height: 110px; margin: 0; pa万古神帝dding: 5px 20px; border: 0; } .form-table .user-avatar-wrap .user-avatar-btn{ width: 80px; margin: 5px 0; padding: 0; border: 0; fWordPressont-size: 14px; } &lphp源码安装t;/style&g武炼巅峰t; <完美世界script> (function($){ // 有 upload_files 权限媒体库选择图片文件 $(php源码网'body').on('click', '.media-library-upload', function(e) { // 阻止事件默认行为 e.preventDephp源码教程fault(); var user_cap = $(this).data('user_cap'); if (user_cap=="NO"){alert("抱歉,你没有上传图片的权限!")} var item_type = $(this)卧底记者成二把手.data('item_type'); var input_tphp源码网ag = $('#avatar'); var img_tag =完美世界 $('#user-avatar-new'); //唤起 WordPress 默认媒体上传 custom_uploader = wp.mediphp源码网a.frames.local_avatar_frame = wp.media({ title: '选择图片', library: { type: 'image' }, button: { text:php源码手机 '选择图片' }, multiple: false }); wp.me卧底记者成二把手dia.frames.local_avatar_frame.on('se微博lect', function() { vwordpress主题ar attachmphp源码网站ent = wp.media.frames.local_avatar_frame.state().get('selection').first().towordpress怎么看别人的文章JSON(); var img_value = (item_type == 'url') ? attachment.url : attachment.id; // 将图像路径赋值给 inputwordpress是啥东西 的wordpress官网入口 value input_tag.val(img_value); iwordpress是啥东西mg_tag.万古神帝飞天鱼a卧底记者成二把手ttr('src', imphp源码网站g_value); }); wp.media.frames.local_avaphp源码分享tar_frame.open(); }); // 无 upload_files 权限本地上传图片文件 $('body').on('changephp源码安装', '.local-avatar-upload', function(e) { var file = this.files[0]; // 限制文件类型 var fileType = /^image\//; if ( !fileType.test(file.type) ) { alphp源码分享ert("请选择一张W图片!"); $(this).val('') return; } //限制文件大小 var imgSize = fPHP源码ile.size; if(imphp源码加密gSize> 200*1024php源码安装){ alert('上传的图片不得php源码安装大于2万相之王00KB!'); $(this).val('') return false; } // 预览图片 filePath = URL.createObjectURL(file); $('#user-avatar-new').attr('src', filePathwordpress狮子歌歌斯文败类); }); })(jQuery) </script> <?php });
引wordpress入 fn_local_avatar.php
将代码丢到主题目录下,然后在主题 functions.php 中直接引入即可,引入代码如下,注意修改路径!
// 后台自定义上传头像 require gWet_template_direcwordpresstoryWordPress() . '武炼巅峰/functions/fn_local_avatar.php';
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
请登录后查看评论内容