# DiscourseConnect SSO force avatar update programatically

**URL:** https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829
**Category:** WordPress
**Tags:** sso
**Created:** [June 26, 2024, 4:07pm UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829 "2024-06-26T16:07:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Shauny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/shauny/32/362012_2.png) [@Shauny](https://meta.discourse.org/u/Shauny)
#### Post date: [June 26, 2024, 4:07pm UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829/1 "2024-06-26T16:07:24Z")

</div>

I have this all set up and working so if someone changes their avatar on my WordPress site and then logs out of the forum and logs in again, it updates the avatar on Discourse.

But people don’t know to log out, and that’s quite annoying, so I’d like to trigger it myself. I have a hook that fires when someone updates their avatar. What can I add for it to send the new avatar to Discourse directly?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [June 27, 2024, 6:54am UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829/2 "2024-06-27T06:54:42Z")

</div>

Hey @Shauny. Which avatars plugin are you using to manage avatars, and how have you set up this?

> [@Shauny](#):
>
> I have a hook that fires when someone updates their avatar

---

<div class="post-metadata">

### Author: ![Shauny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/shauny/32/362012_2.png) [@Shauny](https://meta.discourse.org/u/Shauny)
#### Post date: [June 27, 2024, 7:49am UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829/3 "2024-06-27T07:49:21Z")

</div>

I’m using Ultimate Member for the avatars, I’ve found the right hook which fires once a user uploads one, just need to then send it to Discourse.

Here’s the action:

```plaintext

add_action("um_after_upload_db_meta_profile_photo","um_custom_after_upload_profile_photo", 10, 1 );
function um_custom_after_upload_profile_photo( $user_id ){
    // do something
}

```

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [June 27, 2024, 8:20am UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829/4 "2024-06-27T08:20:02Z")

</div>

Great, so you want to do the following

```php
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;

add_action("um_after_upload_db_meta_profile_photo", "um_custom_after_upload_profile_photo", 10, 1 );
function um_custom_after_upload_profile_photo( $user_id ){
    $avatar_url = // Ultimate Member avatar URL
    $params = array(
        'external_id' => $user_id,
        'avatar_url' => $avatar_url,
        'avatar_force_update' => 'true'
     )
    DiscourseUtilities::sync_sso_record( $params, $user_id );
}

```

You’ll need to figure out the URL for the avatar on Wordpress (you’ll perhaps find this in the Ultimate Member documentation) and set that to the variable `$avatar_url`.

---

<div class="post-metadata">

### Author: ![Shauny](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/shauny/32/362012_2.png) [@Shauny](https://meta.discourse.org/u/Shauny)
#### Post date: [June 27, 2024, 3:52pm UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829/5 "2024-06-27T15:52:01Z")

</div>

I had to add into the params

`'external_id' => $user_id,`

But other than that, it works. Thanks!

(However now I have the issue that the hook I was given is fired _before_ the file is moved, so it gets the old avatar… but that’s not your fault 😅)

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 27, 2024, 3:52pm UTC](https://meta.discourse.org/t/discourseconnect-sso-force-avatar-update-programatically/313829/6 "2024-07-27T15:52:28Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
