# Accessing Discourse user avatar in WP functions.php

**URL:** https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390
**Category:** WordPress
**Created:** [22 באוגוסט,‏ 2018,‏ 9:47am UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390 "2018-08-22T09:47:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Lucas\_Young](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lucas_young/32/109456_2.png) [@Lucas\_Young](https://meta.discourse.org/u/Lucas_Young)
#### Post date: [22 באוגוסט,‏ 2018,‏ 9:47am UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/1 "2018-08-22T09:47:58Z")

</div>

Hi Guys

I’m new to working with Discourse and am working on some changes to a Wordpress site where Discourse is installed via the [WP Discourse](https://github.com/discourse/wp-discourse) plugin.

All I need to do is get the avatar for a user via their Wordpress ID and show it on a page I’m building. Is this a meta field I can query in WP or is there a function I need to call via functions.php to get this?

Many thanks in advance for the assistance.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [4 בספטמבר,‏ 2018,‏ 6:53pm UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/2 "2018-09-04T18:53:51Z")

</div>

Sorry for the delayed response to this. The [WP Discourse](https://github.com/discourse/wp-discourse) plugin doesn’t save the Discourse user’s avatar URL. When it displays avatars in comments, it uses the avatar URL that is returned from Discourse with the comments, and sources the avatar from the Discourse site.

Later this week, I’ll be updating the plugin to add a few extra [hooks](https://codex.wordpress.org/Plugin_API). If you are using your WordPress site as either the SSO provider, or the SSO client for Discourse, there will be a hook added that you could use to save the users `avatar_url` as user metadata on WordPress. You could then retrieve the avatar URL with

`get_user_meta( $user_id, 'your_field_name' );`

You would need to write the WordPress code to make use of the `avatar_url`. If this would be useful for you, I can give you some more details about it.

---

<div class="post-metadata">

### Author: ![Lucas\_Young](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lucas_young/32/109456_2.png) [@Lucas\_Young](https://meta.discourse.org/u/Lucas_Young)
#### Post date: [4 בספטמבר,‏ 2018,‏ 10:05pm UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/3 "2018-09-04T22:05:51Z")

</div>

Thanks Simon!  
I’ll have a play once the hooks are in place 🙂

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [8 בספטמבר,‏ 2018,‏ 1:27am UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/5 "2018-09-08T01:27:42Z")

</div>

In the latest version of the plugin (1.7.5), I’ve added a `wpdc_after_sync_sso` action hook that you can use to retrieve information from the Discourse user object. This hook will only work if you are using your WordPress site as the SSO Provider for Discourse, and have also enabled the `Create or Sync Discourse Users on Login` option. Next week I will look into adding a similar hook for when WordPress is functioning as the SSO Client.

Any function that you hook into `wpdc_after_sync_sso` will be passed up to two parameters: the Discourse user object, and the WordPress user’s ID.

You can use the hook to save the user’s Discourse avatar URL by adding something like this to a plugin, or to your theme’s `functions.php` file:

```php
add_action( 'wpdc_after_sync_sso', 'wpdc_custom_action_after_sync_sso', 10, 2 );
function wpdc_custom_action_after_sync_sso( $discourse_user, $user_id ) {
    // You could just substitute your Discourse URL here, instead of getting it from the options array.
    $discourse_url = \WPDiscourse\Utilities\Utilities::get_options()['url'];
    $avatar_template = str_replace( '{size}', '60', $discourse_user->avatar_template );
    $avatar_url = esc_url_raw( "{$discourse_url}{$avatar_template}" );
    update_user_meta( $user_id, 'discourse_avatar_template', $avatar_url );
}

```

---

<div class="post-metadata">

### Author: ![Genyus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/genyus/32/119655_2.png) [@Genyus](https://meta.discourse.org/u/Genyus)
#### Post date: [18 באפריל,‏ 2019,‏ 9:14am UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/6 "2019-04-18T09:14:58Z")

</div>

@simon was the client-mode hook ever implemented?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [18 באפריל,‏ 2019,‏ 6:17pm UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/7 "2019-04-18T18:17:09Z")

</div>

No, a similar hook has not been added for when WordPress is the SSO client. Unfortunately, it is not as straightforward to add the hook for this case as it was for the case of when WordPress is the SSO provider.

I’ll be looking at the SSO client code over the next week. If there’s anyway I can add a hook that will allow the Discourse user to be retrieved after logging in via Discourse, I will add it.

---

<div class="post-metadata">

### Author: ![squarepants](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/squarepants/32/138311_2.png) [@squarepants](https://meta.discourse.org/u/squarepants)
#### Post date: [30 באפריל,‏ 2019,‏ 8:10am UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/8 "2019-04-30T08:10:16Z")

</div>

Any progress on this one? I am also trying to display user avatar selected in discourse to a WordPress site and stumbled upon this post. (wordpress as sso client)

---

<div class="post-metadata">

### Author: ![Lucas\_Young](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lucas_young/32/109456_2.png) [@Lucas\_Young](https://meta.discourse.org/u/Lucas_Young)
#### Post date: [10 במאי,‏ 2019,‏ 8:42am UTC](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/9 "2019-05-10T08:42:38Z")

</div>

Thanks Simon. It would be great to be able to retrieve the user’s profile url as well, starting only with the user’s WP iD 🙂  
Cheers
