and in my settings in discourse wp i am using discourse connect provider
hello @simon @Firepup650 after a long time i need help again
here is my code
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
function mepr_capture_new_member_signup_completed($event) {
$user = $event->get_data();
$user_id = $user->ID;
// Get user data based on user ID
$user_data = get_userdata($user_id);
// Log debug information about the user data
log_debug_info("debug_log.txt", "User Data: ", print_r($user_data, true));
// Check if user data exists
if ($user_data) {
// Get the user roles
$roles = $user_data->roles;
// Check if the user does not have the "browns" role or does have it
if (!in_array('browns', $roles) || in_array('browns', $roles)) {
// Remove all existing roles
foreach ($roles as $role) {
$user_data->remove_role($role);
}
// Add the "browns" role
$user_data->add_role('browns');
log_debug_info("debug_log.txt", "Assigned Browns Role: ", print_r($user_data, true));
if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
// Add user to the Discourse group for "browns"
$s2member_level_1_groups = get_option('s2member_level_1_groups', '');
log_debug_info("debug_log.txt", "s2member level 1 groups: ", print_r($s2member_level_1_groups, true));
$result = DiscourseUtilities::add_user_to_discourse_group($user_id, $s2member_level_1_groups);
log_debug_info("debug_log.txt", "Discourse Group Addition Result: ", print_r($result, true));
}
// Additional SSO parameters if needed
$sso_params = array();
$sso_params = apply_filters('wpdc_sso_params', $sso_params, $user_data);
} else {
// Collect all level groups
$s2member_level_1_groups = get_option('s2member_level_1_groups', '');
$s2member_level_2_groups = get_option('s2member_level_2_groups', '');
$s2member_level_3_groups = get_option('s2member_level_3_groups', '');
$s2member_level_4_groups = get_option('s2member_level_4_groups', '');
// Combine all groups into one array
$s2member_level_groups = array_merge(
explode(',', $s2member_level_1_groups),
explode(',', $s2member_level_2_groups),
explode(',', $s2member_level_3_groups),
explode(',', $s2member_level_4_groups)
);
// Remove duplicates
$s2member_level_groups = array_unique($s2member_level_groups);
// Remove user from all collected groups
foreach ($s2member_level_groups as $group) {
$result = DiscourseUtilities::remove_user_from_discourse_group($user_id, $group);
// You may handle $result as needed
}
}
} else {
// Handle cases where $user_id or $role is not provided
// This could be logged or handled differently based on your requirements
// For now, let's log a message
error_log("User ID or role not provided in user_registered_callback.");
}
}
add_action('mepr-event-member-signup-completed', 'mepr_capture_new_member_signup_completed');
now the matter is that it is not adding user to discourse group and here is the output of the code
=== User Data: ===
WP_User Object
(
[data] => stdClass Object
(
[ID] => 457
[user_login] => sibahmed
[user_pass] => $P$BTVeX1n6d40NtHBCiQqzSphMolH/hH0
[user_nicename] => sibahmed
[user_email] => sibahmed82@gmail.com
[user_url] =>
[user_registered] => 2024-07-30 17:35:51
[user_activation_key] =>
[user_status] => 0
[display_name] => sib ahmed
)
[ID] => 457
[caps] => Array
(
[subscriber] => 1
[browns] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => subscriber
[1] => browns
)
[allcaps] => Array
(
[read] => 1
[level_0] => 1
[upload_files] => 1
[access_s2member_level0] => 1
[subscriber] => 1
[browns] => 1
)
[filter] =>
[site_id:WP_User:private] => 1
)
=== Assigned Browns Role: ===
WP_User Object
(
[data] => stdClass Object
(
[ID] => 457
[user_login] => sibahmed
[user_pass] => $P$BTVeX1n6d40NtHBCiQqzSphMolH/hH0
[user_nicename] => sibahmed
[user_email] => sibahmed82@gmail.com
[user_url] =>
[user_registered] => 2024-07-30 17:35:51
[user_activation_key] =>
[user_status] => 0
[display_name] => sib ahmed
[user_level] => 0
)
[ID] => 457
[caps] => Array
(
[browns] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => browns
)
[allcaps] => Array
(
[read] => 1
[browns] => 1
)
[filter] =>
[site_id:WP_User:private] => 1
)
=== s2member level 1 groups: ===
Member
=== Discourse Group Addition Result: ===
WP_Error Object
(
[errors] => Array
(
[wpdc_response_error] => Array
(
[0] => An invalid response was returned from Discourse
)
)
[error_data] => Array
(
[wpdc_response_error] => Array
(
[http_code] => 404
[http_body] => {"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}
)
)
[additional_data:protected] => Array
(
)
)
I’m not doing any WordPress development at the moment, so I can’t test your code. I think the issue is here:
$result = DiscourseUtilities::add_user_to_discourse_group($user_id, $s2member_level_1_groups);
If I’m reading the logs correctly, that call is returning:
[error_data] => Array
(
[wpdc_response_error] => Array
(
[http_code] => 404
[http_body] => {"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}
)
I’m guessing that the problem is that there isn’t a group with the name "Member"
on your Discourse site.
I’m not sure what you are doing here, but I don’t think it’s related to the error:
// Additional SSO parameters if needed
$sso_params = array();
$sso_params = apply_filters('wpdc_sso_params', $sso_params, $user_data);
Hey @Haseeb_Ahmed, I’ve moved this topic to dev as the thrust of it is assistance with PHP coding. I’ll perhaps have time to take a closer look at your code next week.
hi @simon even if i change the group name it is still giving this response
=== s2member level 1 groups: ===
staff
=== Discourse Group Addition Result: ===
WP_Error Object
(
[errors] => Array
(
[wpdc_response_error] => Array
(
[0] => An invalid response was returned from Discourse
)
)
[error_data] => Array
(
[wpdc_response_error] => Array
(
[http_code] => 404
[http_body] => {"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}
)
)
[additional_data:protected] => Array
(
)
)