Hello there i have a problem with discourse forum which is https://community.over40connect.com/ that is connected with my website https://over40connect.com/staging/ using wp discourse plugin and i have created a function to add user to the discourse group when user role change but it is not adding that user into the discourse group here is the code
function add_user_to_discourse_group_on_role_change($user_id, $role) {
// Check if the role change is relevant for your case
// For example, you might want to run this only if the new role is 's2member_level1'
//if ($role === 's2member_level1') {
// Set the Discourse API endpoint
$discourse_endpoint = 'https://community.over40connect.com/groups/2/members.json';
// Set up the data to be sent
$data = array(
'usernames' => array('haseebdeveloper'), // The username of the WordPress user to add
);
// Make the API request
$response = wp_remote_post($discourse_endpoint, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/json',
'Api-Key' => 'REDACTED', // Add your Discourse API key here
),
'body' => json_encode($data),
'cookies' => array(),
));
// Check for errors and handle the response
if (!is_wp_error($response)) {
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code === 200) {
$response_body = wp_remote_retrieve_body($response);
error_log('Discourse API Response: ' . $response_body); // Log the response
// Handle the response as needed
} else {
error_log('Discourse API Error: Unexpected response code - ' . $response_code); // Log the error
// Handle the error
}
} else {
$error_message = $response->get_error_message();
error_log('Discourse API Error: ' . $error_message); // Log the error
// Handle the error
}
//}
}