PaulJames
PaulJames

Reputation: 95

Can't change wordpress user avatar programmatically

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

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);

Upvotes: 2

Views: 3140

Answers (1)

Hermes
Hermes

Reputation: 2898

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:

  • Implement custom avatar based on a given url, that is stored in the user meta data.
  • Implement a new default gravatar that can be used by multiple users
  • Implement a plugin to allow any user to upload their own avatar.

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;

}

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;
}

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/

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)

Upvotes: 2

Related Questions