Accessing Discourse user avatar in WP functions.php

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