Thank you, Angus. Your solution is elegant. I followed a slightly longer path -
- I used the Data explorer API to get the approval status
- Link it to the template_redirect hook to check the approval status on login
- Update the custom field in WP on login
- 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
?>