How to know Discourse user is unapproved in Wordpress?

I want to create a user automatically in Discourse when new registration happens in WordPress but I don’t want to give them access before they complete an application form.

I am able to use sync on login and must approve users settings to achieve most of this but I don’t know how to retrieve if the user is approved in Discourse?

Is there a WP user meta field that Discourse uses to store Discourse user approved status? If not, do I need to use an API to get the information from Discourse?

My goal - I want to show a button on my dashboard in WP with a dynamic link that changes based on approved or unapproved status.
Unapproved user - dynamic link directs to application form on WP
Approved user - dynamic link directs to the discourse community.

I looked at a few posts but could not find a solution. Help appreciated!

1 Like

I figured out how to make this happen by writing a data explorer query and calling it via API. It works fine but now I am making a call between two systems which is obviously slower than having the data saved in WP.

If the page load is slow, I would use a hook to store the approval data in user custom field on my own. Before I go ahead, it would be good to know if WP-Discourse Plugin already does that.

Hey @Himanshu_Singh, the short answer is that the WP Discourse plugin does not store the approval status of the Discourse user.

I’d suggest you take a look at the wpdc_after_sync_sso action, which is fired after the user record is synced after a user logs into Wordpress.

If you stick a log in there, e.g.

function wpdc_after_sync_sso_callback($discourse_user, $user_id) {
    error_log(print_r($discourse_user, true));
}
add_action( 'wpdc_after_sync_sso', 'wpdc_after_sync_sso_callback', 10, 2 );

You’ll see that the discourse_user object contains the approval status of the user, e.g.

stdClass Object
(
    [id] => 43
    [username] => angus4
    [name] => angus4
    ...
    [approved] =>
)

Which you could then store yourself in a user custom field and use to create your dynamic link.

1 Like

Thank you, Angus. Your solution is elegant. I followed a slightly longer path -

  1. I used the Data explorer API to get the approval status
  2. Link it to the template_redirect hook to check the approval status on login
  3. Update the custom field in WP on login
  4. Prevent the API to run again if the user is approved.
add_action('template_redirect', 'update_discourse_approval_status',1);
function update_discourse_approval_status () {
  
  global $post;
  $post_slug = $post->post_name;
  if ($post_slug == 'page-slug'){
    $current_user_id = get_current_user_id();
	$approval_status_stored_in_wp = get_user_meta($current_user_id, 'custom-field', true);
    if( $approval_status_stored_in_wp != 1) {
      $user_discourse_id = (int)get_user_meta($current_user_id,'discourse_sso_user_id')[0];
	//api credentials,
	$api_key = 'api-key';
	$api_username = 'name';

	$discourse_user_param_string = 'params={"user_param":';
	$discourse_user_param_string .= '"';
	$discourse_user_param_string .= $user_discourse_id;
	$discourse_user_param_string .= '"}';

	//header is a must for Discourse API. 
	$headers = array("Content-Type: multipart/form-data;","Api-Key:$api_key","Api-Username:$api_username",);

	$url = 'https://community.showprowess.com/admin/plugins/explorer/queries/<query-number>/run';
	$ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $discourse_user_param_string);
    $result = curl_exec( $ch );

	if ( curl_errno( $ch ) !== 0 ) {
    // Handle error, call curl_close( $ch ) and return.
	echo 'error';
	curl_close ($ch);
	return;
	}

	curl_close( $ch );

	$decoded_result = json_decode( $result, true );

	$user_approved = $decoded_result['rows'][0][0];
	$updated = update_user_meta( $current_user_id, 'custom-field', $user_approved);
    } 
  } //close if statement to check data stored in custom field
} // close if to check the right page 
          
?>
1 Like