# How to know Discourse user is unapproved in Wordpress?

**URL:** <https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228>\
**Category:** WordPress\
**Created:** [June 9, 2021, 12:22am UTC](https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228 "2021-06-09T00:22:50Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Himanshu\_Singh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/himanshu_singh/32/216438_2.png) [@Himanshu\_Singh](https://meta.discourse.org/u/Himanshu_Singh)\
**Post date:** [June 15, 2021, 2:52pm UTC](https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228/4 "2021-06-15T14:52:46Z")

</div>

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

1. I used the [Data explorer](https://meta.discourse.org/t/32566?silent=true) 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.

```plaintext
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 
          
?>

```

---

_[View the full topic](https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228)._
