当前位置:首页>网站建设>代码实现WordPress添加本地头像功能代替Gravatar头像自动草稿

代码实现WordPress添加本地头像功能代替Gravatar头像自动草稿

释放双眼,带上耳机,听听看~!

要在WordPress中添加本地头像功能以替代Gravatar头像,您可以按照以下步骤操作:

创建一个自定义用户资料字段:首先,您需要创建一个用户资料字段,用于上传本地头像。您可以使用WordPress的add_action函数将以下代码添加到主题的functions.php文件中:

function custom_user_profile_fields($user) {
    ?>
    <h3><?php _e('Local Avatar', 'yourtextdomain'); ?></h3>
    <table class="formtable">
        <tr>
            <th>
                <label for="local_avatar"><?php _e('Upload Avatar', 'yourtextdomain'); ?></label>
            </th>
            <td>
                <input type="file" name="local_avatar" id="local_avatar" accept="image/" />
                <br />
                <span class="description"><?php _e('Upload a custom avatar image.', 'yourtextdomain'); ?></span>
            </td>
        </tr>
    </table>
    <?php
}
add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');

保存和显示本地头像:接下来,您需要处理上传的本地头像并在用户资料页面上显示它。使用以下代码将处理和显示逻辑添加到functions.php文件:

function save_custom_user_profile_fields($user_id) {
    if (current_user_can('edit_user', $user_id)) {
        if (isset($_FILES['local_avatar']['name']) && $_FILES['local_avatar']['name']) {
            $upload_dir = wp_upload_dir();
            $avatar_file = $_FILES['local_avatar']['tmp_name'];
            $avatar_name = sanitize_file_name($_FILES['local_avatar']['name']);
            $avatar_path = $upload_dir['path'] . '/' . $avatar_name;
            move_uploaded_file($avatar_file, $avatar_path);
            update_user_meta($user_id, 'local_avatar', $avatar_path);
        }
    }
}
add_action('personal_options_update', 'save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');
 
function display_local_avatar($avatar, $id_or_email, $size) {
    if (is_numeric($id_or_email)) {
        $user_id = $id_or_email;
    } elseif (is_object($id_or_email)) {
        if (!empty($id_or_email>user_id)) {
            $user_id = $id_or_email>user_id;
        }
    } else {
        $user = get_user_by('email', $id_or_email);
        if ($user) {
            $user_id = $user>ID;
        }
    }
    if (isset($user_id)) {
        $local_avatar = get_user_meta($user_id, 'local_avatar', true);
        if ($local_avatar) {
            $avatar = '<img alt="" src="' . esc_url($local_avatar) . '" class="avatar avatar' . $size . '" height="' . $size . '" width="' . $size . '" />';
        }
    }
    return $avatar;
}
add_filter('get_avatar', 'display_local_avatar', 10, 3);

这些代码会创建一个名为”Local Avatar”的自定义字段,允许用户上传本地头像。上传的头像将保存在WordPress的媒体库中。然后,使用get_avatar过滤器来显示本地头像。

欢迎访问秀主题博客,分享简单实用WP教程

确保替换代码中的’yourtextdomain’以匹配您的主题或插件的文本域。还要确保您的主题支持上传图像,并且具有合适的CSS样式以呈现头像。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
网站建设

WordPress添加本地头像功能代替Gravatar头像功能

2025-6-15 8:18:14

网站建设

WordPress 随机显示本地头像加速访问

2025-6-17 9:35:41

温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:971318156@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索