Sorry, this is so long. I am using a checkbox User Field to manage secondary user groups from the front end.
I do not want my moderators to have back-end access, just todo this one task. My platform is much more SEC by doing it from the front side
I have created a custom checkbox field that is only visible and editable by moderators and admins. The end user does not have permission to edit the field
I already have the checkbox field ID and the corresponding user group IDs.
I need the specific code snippet or hook to ensure that when a moderator selects a checkbox (on the user's profile) and saves the user profile, the user is automatically added to the secondary user group in the database. The end user cannot edit this.
1. Custom checkbox field created in User Fields.
2. Permissions set to moderator and admin only.
3. Field ID and user group IDs are identified.
4. Goal is to sync the custom checkbox to save when the moderator updates, when the profile is saved.
Please see the current code I am using below.
I need the specific code snippet or hook to ensure that when a moderator selects a checkbox and saves the user profile, the user is automatically added to that secondary user group in the database.
Please see the current code I am using below.
add_action('wpforo_after_update_user', function($user_id) {
// 1. GATEKEEPER SECURITY: Only allow Admins or Gatekeepers
if (!current_user_can('moderate_comments') && !current_user_can('manage_options')) return;
// 2. THE MAP: "Label in Field" => "wpForo Group ID"
$field_key = 'field_6d80de0';
$group_map = [
'John 17 Youth' => 20,
'Churches' => 21,
'Organizations' => 23,
'Retailers' => 24
];
// 3. CAPTURE DATA FROM THE WPFORO-SPECIFIC ARRAY
$selected_labels = [];
if (isset($_POST['wpforo_profile'][$field_key])) {
$selected_labels = (array)$_POST['wpforo_profile'][$field_key];
} elseif (isset($_POST[$field_key])) {
$selected_labels = (array)$_POST[$field_key];
}
// Safety: If the field isn't in the submission, don't change anything
if (empty($selected_labels) && !isset($_POST['wpforo_profile'])) return;
// 4. SYNC TO WPFORO SECONDARY GROUPS
if (function_exists('WPF')) {
$member = WPF()->member->get_member($user_id);
if (!$member) return;
// Current secondary groups (wpForo stores these as an array of IDs)
$current_secondary = !empty($member['secondary_group_ids']) ? (array)$member['secondary_group_ids'] : [];
foreach ($group_map as $label => $group_id) {
if (in_array(trim($label), array_map('trim', $selected_labels))) {
// Add if checked and missing
if (!in_array($group_id, $current_secondary)) {
$current_secondary[] = $group_id;
}
} else {
// Remove if unchecked (only for these 4 managed groups)
$current_secondary = array_diff($current_secondary, [$group_id]);
}
}
// 5. THE FINAL FIRE: Update the database and clear forum cache
WPF()->member->update($user_id, ['secondary_group_ids' => array_unique(array_filter($current_secondary))]);
WPF()->member->reset($user_id);
}
}, 10);
Thank you!
