SSO Client avatars


(Adam Adam) #1

I recently found this topic. Everything in this guide is now already possible using the discourse plugin, except for the “Update Avatar” feature

And was wondering if it is possible to use the following code on my wordpress site. So that word press automatically updates users avatars based on what their avatar is on discourse.

/**
  Avatar Sync
 */
add_action( 'ac_discourse_avatar_sync_event', 'ac_discourse_avatar_sync' );

function ac_discourse_avatar_sync( $user_id ) {

	// Define variables
	$discourse_url = DISCOURSE_URL;
	$userdata = get_userdata( $user_id );

	$url = sprintf(
		'%s/users/%s/activity.json',
		$discourse_url,
		$userdata->user_login
	);

	// cURL
	$ch = curl_init();
	curl_setopt( $ch, CURLOPT_URL, $url );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	$body = curl_exec( $ch );
	curl_close( $ch );

	// Interpret results
	$json = json_decode( $body, true );

	// Update user meta
	if ( isset( $json['user']['avatar_template'] ) ) {

		update_user_meta( $user_id, 'discourse_avatar', $json['user']['avatar_template'] );

	}

}

Questions

  1. Will this still work? the topic is from 2015

  2. where on wordpress should I add this code?


(Steven Greco) #2

You would add it to your functions.php file. However the plugin has a force update avatar function from wordpress to Discourse.

Personally I just tell people to use Gravatar since both Wordpress and Discourse support it.


(Sami Syed) #3

What does this mean?


(Steven Greco) #4

There is a function built into Discourse SSO called Avatar_force_update. The wordpress plugin uses this function to synce the avatar from Wordpress to Discourse. It’s in the opposite direction than from what I think you are going for but it can be enabled from the settings.


(Sami Syed) #5

And how would I use gravatar in this case?


(Steven Greco) #6

If the user has a gravatar setup with the same email the used to register on wordpress. Wordpress by default will use the gravatar. Discourse will do the same.