Title: Setting user&#039;s avatar programmatically?
Last modified: August 24, 2016

---

# Setting user's avatar programmatically?

 *  [tyteen4a03](https://wordpress.org/support/users/tyteen4a03/)
 * (@tyteen4a03)
 * [11 years ago](https://wordpress.org/support/topic/setting-users-avatar-programmatically/)
 * How do I set a user’s avatar programmatically? I’ve got an image file and would
   like to use it as an user’s avatar.
 * [https://wordpress.org/plugins/wp-user-avatar/](https://wordpress.org/plugins/wp-user-avatar/)

Viewing 1 replies (of 1 total)

 *  [Emulator000](https://wordpress.org/support/users/emulator000/)
 * (@emulator000)
 * [11 years ago](https://wordpress.org/support/topic/setting-users-avatar-programmatically/#post-6129936)
 * Hi,
    for set an image to the user avatar simply do this: delete_metadata(‘post’,
   null, ‘_wp_attachment_wp_user_avatar’, $user_id, true);
 * update_user_meta($user_id, ‘_wp_attachment_wp_user_avatar’, $attach_id);
 * update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id) . ‘user_avatar’, 
   $attach_id);
 * For create an attachment:
    $upload_dir = wp_upload_dir();
 * $wp_filetype = wp_check_filetype(basename($file), null);
 * $attachment = array(
    ‘guid’ => $upload_dir[‘url’] . ‘/’ . basename($file), ‘
   post_mime_type’ => $wp_filetype[‘type’], ‘post_title’ => preg_replace(‘/\.[^.]
   +$/’, ”, basename($file)), ‘post_content’ => ”, ‘post_status’ => ‘inherit’ );
 * $attach_id = wp_insert_attachment($attachment, $file);
    $attach_data = wp_generate_attachment_metadata(
   $attach_id, $file);
 * wp_update_attachment_metadata($attach_id, $attach_data);

Viewing 1 replies (of 1 total)

The topic ‘Setting user's 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/)

 * 1 reply
 * 2 participants
 * Last reply from: [Emulator000](https://wordpress.org/support/users/emulator000/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/setting-users-avatar-programmatically/#post-6129936)
 * Status: not resolved