Title: How to Set Avatar Programmatically
Last modified: October 27, 2021

---

# How to Set Avatar Programmatically

 *  Resolved [aaronmckeon](https://wordpress.org/support/users/aaronmckeon/)
 * (@aaronmckeon)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-set-avatar-programmatically/)
 * I am trying to allow users to upload their avatar on the front-end of the site.
   Since this plugin does not have that built-in functionality, I’m going to have
   them upload an image and then I’d like some PHP code to assign it as their avatar
   afterwards.
 * Would this be the most appropriate code to use once the upload completes?
 *     ```
       $avatar_id = **ID OF THE IMAGE FILE UPLOADED ELSEWHERE**;
       $sla = new Simple_Local_Avatars();
       $sla->assign_new_user_avatar( (int) $avatar_id, $user->ID );
       ```
   
 * Will this work if the user already has an avatar? Thanks for any help!
 * – Aaron
    -  This topic was modified 4 years, 7 months ago by [aaronmckeon](https://wordpress.org/support/users/aaronmckeon/).

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

 *  Thread Starter [aaronmckeon](https://wordpress.org/support/users/aaronmckeon/)
 * (@aaronmckeon)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-set-avatar-programmatically/#post-15012723)
 * I was able to figure this out. I’ll post a separate topic showing people how 
   I was able to allow users to upload on the front-end.
 *  Plugin Contributor [Darin Kotter](https://wordpress.org/support/users/dkotter/)
 * (@dkotter)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-set-avatar-programmatically/#post-15012771)
 * [@aaronmckeon](https://wordpress.org/support/users/aaronmckeon/) Sounds like 
   you got this working, which is great, but if you’re trying to programmatically
   set an avatar, that is the right approach.
 * I do wonder though if it would be worth having a utility function that can be
   used to call that `assign_new_user_avatar` method, similar to our `get_simple_local_avatar`.
   This would simplify things a bit, ensuring you don’t have to create a new instance
   of `Simple_Local_Avatars` yourself. I’ll put this idea into our roadmap. Thanks!

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

The topic ‘How to Set Avatar Programmatically’ is closed to new replies.

 * ![](https://ps.w.org/simple-local-avatars/assets/icon.svg?rev=2783752)
 * [Simple Local Avatars](https://wordpress.org/plugins/simple-local-avatars/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/simple-local-avatars/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/simple-local-avatars/)
 * [Active Topics](https://wordpress.org/support/plugin/simple-local-avatars/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/simple-local-avatars/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/simple-local-avatars/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Darin Kotter](https://wordpress.org/support/users/dkotter/)
 * Last activity: [4 years, 7 months ago](https://wordpress.org/support/topic/how-to-set-avatar-programmatically/#post-15012771)
 * Status: resolved