You can use something like this and hook it into the wp_login action (remove the public keyword.)
public function lookup_discourse_user( $username, $wp_user ) {
$base_url = $this->options['url'];
$api_key = $this->options['api-key'];
$api_username = $this->options['publish-username'];
if ( empty( $base_url ) || empty( $api_key ) || empty( $api_username ) ) {
return new \WP_Error( 'discourse_configuration_error', 'The WP Discourse plugin has not been properly configured.' );
}
// Try to get the user by external_id.
$external_user_url = esc_url_raw( $base_url . "/users/by-external/$wp_user->ID.json" );
$external_user_url = add_query_arg( array(
'api_key' => $api_key,
'api_username' => $api_username,
), $external_user_url );
$response = wp_remote_get( $external_user_url );
if ( ! DiscourseUtilities::validate( $response ) ) {
return new \WP_Error( 'discourse_response_error', 'There has been an error in retrieving the user data from Discourse.' );
}
$user_data = json_decode( wp_remote_retrieve_body( $response ), true );
return $user_data;
}
You can get the options in your theme or a plugin by calling $options = WPDiscourse\Utilities\Utilities::get_options(). You can use the validate method by calling WPDiscourse\Utilities\Utilities::validate( $response ).
In the next update to the plugin, I might be able to add an option to sync user_data on login. It probably won’t sync all the data that you’re looking for, but an action could be added that would let you access the user_data.