Title: Change Avatar Programmatically
Last modified: July 27, 2019

---

# Change Avatar Programmatically

 *  Resolved [EthicalStores](https://wordpress.org/support/users/ethicalstores/)
 * (@ethicalstores)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/change-avatar-programmatically/)
 * 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&#8217](https://myweb.com/Logo-test2.png&#8217);,
   5);

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Ayus Mohanty](https://wordpress.org/support/users/ayusmohanty/)
 * (@ayusmohanty)
 * [6 years, 7 months ago](https://wordpress.org/support/topic/change-avatar-programmatically/#post-12037561)
 * 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](https://wordpress.org/support/users/cyberplayer/)
 * (@cyberplayer)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/change-avatar-programmatically/#post-12532987)
 * 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
       ```
   
 *  [rodriguezpjosue](https://wordpress.org/support/users/rodriguezpjosue/)
 * (@rodriguezpjosue)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/change-avatar-programmatically/#post-12628161)
 * 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.

 * ![](https://ps.w.org/wp-user-avatar/assets/icon-256x256.png?rev=2532486)
 * [Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress](https://wordpress.org/plugins/wp-user-avatar/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-user-avatar/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-user-avatar/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-user-avatar/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-user-avatar/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-user-avatar/reviews/)

 * 3 replies
 * 4 participants
 * Last reply from: [rodriguezpjosue](https://wordpress.org/support/users/rodriguezpjosue/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/change-avatar-programmatically/#post-12628161)
 * Status: resolved