# 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:** 4
**Page:** 1

<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 9, 2021, 12:22am UTC](https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228/1 "2021-06-09T00:22:50Z")

</div>

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!

---

<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 9, 2021, 11:25pm UTC](https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228/2 "2021-06-09T23:25:14Z")

</div>

I figured out how to make this happen by writing a [data explorer](https://meta.discourse.org/t/32566?silent=true) 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.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [June 14, 2021, 12:15am UTC](https://meta.discourse.org/t/how-to-know-discourse-user-is-unapproved-in-wordpress/193228/3 "2021-06-14T00:15:29Z")

</div>

Hey @Himanshu_Singh, the short answer is that the [WP Discourse](https://github.com/discourse/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.

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

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

---

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

```
