Accessing Discourse user avatar in WP functions.php

(Lucas Young) #1

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 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.

(Simon Cossar) #2

Sorry for the delayed response to this. The 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. 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.

3 Likes
(Lucas Young) #3

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

1 Like
(Simon Cossar) #5

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:

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 );
}
2 Likes
(Gary McPherson) #6

@Simon_Cossar was the client-mode hook ever implemented?

(Simon Cossar) #7

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.

1 Like
(Kush) #8

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)

(Lucas Young) #9

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 :slight_smile:
Cheers