• Resolved EthicalStores

    (@ethicalstores)


    I am wanting to change the avatar of several of my users (ideally by their ID), is this possible.

    Something like this – but this is not working for me

    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://myweb.com/Logo-test2.png’, 5);

Viewing 3 replies - 1 through 3 (of 3 total)
  • I am not sure if this would help or not. I was migrating a website from a different platform to wordpress. I had to migrate the users’ profile images also. I had no clue how do I import images to this plugin. So what I did was stored the profile images URL in wp_usermeta for every user and used this filter to show on the frontend. I did it for comments only.

    <?php
    function custom_user_avatar($avatar, $id_or_email = NULL, $size = NULL, $align = NULL, $alt = NULL) {
      if(is_object($id_or_email) && isset($id_or_email->comment_author_email)){
        $comment_user = get_user_by('email',$id_or_email->comment_author_email);
        if(is_object($comment_user)){
          $user_pic_base_url = 'url';
          $user_pic = get_user_meta($comment_user->ID,'author_pic',true);
          if($user_pic){
            $avatar = '<img src="' . $user_pic_base_url . $user_pic . '" width="54" height="54" alt="admin.kh" class="avatar avatar-54 wp-user-avatar wp-user-avatar-54 alignnone photo">';
          }
        }
      }
      return $avatar;
    }
    add_filter('get_wp_user_avatar', 'custom_user_avatar', 1, 5);
    cyberplayer

    (@cyberplayer)

    Hello, this works :

    add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);
    
    function ht1_change_avatar($args, $id_or_email) {
        if($id_or_email == 1) {
            $args['url'] = 'https://uinames.com/api/photos/female/1.jpg';
        }
    
        if($id_or_email == 2) {
            $args['url'] = 'https://uinames.com/api/photos/male/19.jpg';
        }
    
        return $args;
    } // end of function

    Hi!!!
    Reviewing the code of the plugin, i was founded some code lines. That allows complete the task.

    After wp_update_attachment_metadata(….
    replace the code:

    `global $blog_id;
    // Remove old attachment postmeta
    delete_metadata(‘post’, null, ‘_wp_attachment_wp_user_avatar’, $user_id, true);
    // Create new attachment postmeta
    update_post_meta($attach_id, ‘_wp_attachment_wp_user_avatar’, $user_id);
    // Update usermeta
    update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).’user_avatar’, $attach_id);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change Avatar Programmatically’ is closed to new replies.