无法以编程方式更改 wordpress 用户头像 [英] Can't change wordpress user avatar programmatically

查看:34
本文介绍了无法以编程方式更改 wordpress 用户头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改我的 wordpress 网站的几个用户的头像.

I am attempting to change an avatar on several of users of my wordpress site.

我读过最好的方法是使用 WP User Avatar 插件和下面的代码,但不幸的是它不起作用.能否请教一下社区的意见.

I have read that the best way to go would be to use the WP User Avatar plugin and the below code, but unfortunately its not working. Could I ask the advice of the community.

function set_avatar_url($avatar_url, $user_id) {
        global $wpdb;
        $file = upload_product_image($avatar_url);
        $wp_filetype = wp_check_filetype($file['file']);
        $attachment = array(
            'guid' => $file['url'],
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($file['file'])),
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment($attachment, $file['file']);
        $attach_data = wp_generate_attachment_metadata($attach_id, $file['file']);
        wp_update_attachment_metadata($attach_id, $attach_data);
        update_user_meta($user_id, $wpdb->get_blog_prefix() . 'user_avatar', $attach_id);
    }

set_avatar_url('https:/mysite.com/Logo-test2.png', 5);

推荐答案

我不是 100% 确定我遵循您的要求,但我会尝试提供最佳答案,我可以在此问题的有限上下文中给出我的答案.有几种不同的方法:

I'm not 100% sure I follow what you are asking, but I will attempt to provide the best answer I can given the limited context for this question. There are a few different approaches:

  • 根据存储在用户元数据中的给定网址实现自定义头像.
  • 实现可供多个用户使用的新默认头像
  • 实施插件以允许任何用户上传自己的头像.

如果您希望实现使用存储在用户元字段 field_with_custom_avatar 中的图像的给定 url 覆盖用户图像的功能,请将以下代码段添加到您的 函数中.php

If you are looking to implement the ability to override the users image with a given url of an image that's stored in the user meta field field_with_custom_avatar, add the snippet below to your functions.php

add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {

    //If is email, try and find user ID
    if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
        $user  =  get_user_by( 'email', $id_or_email );
        if( $user ){
            $id_or_email = $user->ID;
        }
    }

    //if not user ID, return
    if( ! is_numeric( $id_or_email ) ){
        return $avatar;
    }

    //Find URL of saved avatar in user meta
    $saved = get_user_meta( $id_or_email, 'field_with_custom_avatar', true );
    //check if it is a URL
    if( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
        //return saved image
        return sprintf( '<img src="%" alt="%" />', esc_url( $saved ), esc_attr( $alt ) );
    }

    //return normal
    return $avatar;

}

如果您希望实现拥有新默认"gravatar 的功能,那么您可以将图像上传到您的媒体库(用于设置 $myavatar 的值),然后添加以下内容functions.php 文件的代码片段.

If you are looking to implement the ability to have a new "default" gravatar then you could upload an image to your media library (use to set value of $myavatar) and then add the following code snippet to your functions.php file.

add_filter( 'avatar_defaults', 'wpb_new_gravatar' );
function wpb_new_gravatar ($avatar_defaults) {
     $myavatar = 'http://example.com/wp-content/uploads/2017/01/wpb-default-gravatar.png';
     $avatar_defaults[$myavatar] = "Default Gravatar";
     return $avatar_defaults;
}

如果您正在寻找允许用户上传自己的图片的插件,您可以尝试 https://wordpress.org/plugins/wp-user-avatar/

If you are looking for a plugin to allow user to upload their own images you could try https://wordpress.org/plugins/wp-user-avatar/

注意:我不隶属于这个插件,它有一个很好的评价,300,000+ 次安装,并且测试到 v5.5.2(迄今为止的最新版本)

Note: I'm not affiliated with this plugin, it has a very good rating, 300,000+ installs, and is tested up to v5.5.2 (latest version to date)

这篇关于无法以编程方式更改 wordpress 用户头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆