Title: How to upload avatar programatically?
Last modified: March 14, 2020

---

# How to upload avatar programatically?

 *  Resolved [JP3](https://wordpress.org/support/users/jp3/)
 * (@jp3)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/)
 * Hi!
 * Wonderful plugin. It does an amazing job.
 * On snag I’ve run into and I’m not sure the proper way to handle it because of
   the custom DB tables along with the user meta values.
 * I just want to upload avatars for members programatically without using any frontend,
   and have Avatar Privacy process/handle it the way that it should.
 * I have a directory of images to loop through and intended to do something like
   this per each image/user_id after adding the images to the library and getting
   IDs:
    `update_user_meta($user_id, $wpdb->get_blog_prefix() . ‘user_avatar’, $
   attach_id);` …but it’s now “avatar_privacy_user_avatar” as the meta key, it’s
   an array, and there’s a lot more custom DB stuff behind the scenes. So I’m confused
   how to do this with the plugin.
 * I’m hoping to use a magic “avatar_privacy_upload_avatar()” function I can pass
   the user_id and either the file URI (not uploaded) or attach_id (uploaded), and
   Avatar Privacy do all the magic it has to.
 * If there’s no straightforward way to do this, then I want to suggest this as 
   a feature request.
 * As an added bonus, having the ability to set these programatically:
    “Display
   a Gravatar image for my e-mail address.” & “Allow logged-out comments with my
   profile picture.”
 * a magic function like
    `avatar_privacy_update_user($user_id, $attach_id=NULL,
   $delete_old=TRUE, $display_gravatar=FALSE, $display_logged_out=FALSE)`
 * Thanks for your time.

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

 *  Plugin Author [pepe](https://wordpress.org/support/users/pputzer/)
 * (@pputzer)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/#post-12544485)
 * There is a way to update “user settings” programmatically, but not for uploading
   avatar images. I’ll add it as a feature request on GitHub.
 * Here’s how to access Avatar Privacy’s API:
 *     ```
       $api = \Avatar_Privacy\Core::get_instance();
   
       /**
        * Ensures that the comment author gravatar policy is updated.
        *
        * @param  string $email        The comment author's mail address.
        * @param  int    $comment_id   The comment ID.
        * @param  int    $use_gravatar 1 if Gravatar.com is enabled, 0 otherwise.
        */
       $api->update_comment_author_gravatar_use( $email, $comment_id, $use_gravatar );
       ```
   
 * For the user settings, you don’t need the actual API instance, this is just handled
   by the standard user meta API. However, the meta keys are available as constants
   in `\Avatar_Privacy\Core`:
    - `\Avatar_Privacy\Core::EMAIL_HASH_META_KEY`
    - `\Avatar_Privacy\Core::GRAVATAR_USE_META_KEY`
    - `\Avatar_Privacy\Core::ALLOW_ANONYMOUS_META_KEY`
    - `\Avatar_Privacy\Core::USER_AVATAR_META_KEY`
 * But yes, having convenient function method to update all settings in one go is
   probably a good idea. I’ll have to think about a good way to do it, though. Maybe
   a façade is best here instead of direct use of the `Core` API.
 *  Plugin Author [pepe](https://wordpress.org/support/users/pputzer/)
 * (@pputzer)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/#post-12549850)
 * Hi [@jp3](https://wordpress.org/support/users/jp3/), I’ve no created two enhancement
   requests on GitHub to track this ([#161](https://github.com/mundschenk-at/avatar-privacy/issues/161),
   [#162](https://github.com/mundschenk-at/avatar-privacy/issues/162)). If possible,
   we should continue the discussion there (unless you need more information on 
   the current API).
 *  Thread Starter [JP3](https://wordpress.org/support/users/jp3/)
 * (@jp3)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/#post-12550704)
 * Thanks for the quick and detailed responses!
 * To be honest, I have an entire old VBulletin site I’m migrating over to WP, and
   I have so many other more important things to tackle, that I don’t have the time
   right now to mess with avatars.
 * I WANT to use Avatar Privacy, but for now, I switched over to [Simple User Avatar](https://wordpress.org/plugins/simple-user-avatar/).
   It stores the avatar as the media file id in usermeta [‘mm_sua_attachment_id’].
 * The idea is that I can move along on my site conversion and tackle the bigger
   fish, and when it’s quickly/easily a possibility, switch back to Avatar Privacy
   and move all current [‘mm_sua_attachment_id’] media ids over.
 * Thanks again for the response and potentially adding this “add/update programmatically”
   feature in the future.
 *  Plugin Author [pepe](https://wordpress.org/support/users/pputzer/)
 * (@pputzer)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/#post-13335296)
 * Hi [@jp3](https://wordpress.org/support/users/jp3/) I’m currently working on 
   that API I promised. Since currently user avatars are not part of the media library,
   would prefer to have a file name or the actual image data when setting a user
   avatar? (It would be copied to the standard location in either case.)
 *  Plugin Author [pepe](https://wordpress.org/support/users/pputzer/)
 * (@pputzer)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/#post-13832571)
 * Hi [@jp3](https://wordpress.org/support/users/jp3/), the requested API will land
   in Avatar Privacy 2.4.0. You can [preview the methods here](https://github.com/mundschenk-at/avatar-privacy/blob/master/includes/avatar-privacy/class-core.php#L356).

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

The topic ‘How to upload avatar programatically?’ is closed to new replies.

 * ![](https://ps.w.org/avatar-privacy/assets/icon.svg?rev=2711226)
 * [Avatar Privacy](https://wordpress.org/plugins/avatar-privacy/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/avatar-privacy/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/avatar-privacy/)
 * [Active Topics](https://wordpress.org/support/plugin/avatar-privacy/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/avatar-privacy/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/avatar-privacy/reviews/)

## Tags

 * [programmatically](https://wordpress.org/support/topic-tag/programmatically/)
 * [upload](https://wordpress.org/support/topic-tag/upload/)

 * 5 replies
 * 2 participants
 * Last reply from: [pepe](https://wordpress.org/support/users/pputzer/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/how-to-upload-avatar-programatically/#post-13832571)
 * Status: resolved