uo_groups_role_cleanup
Fires after a user's roles are cleaned up, passing the user ID to hooked functions for further processing.
add_action( 'uo_groups_role_cleanup', $callback, 10, 1 );
Description
Fires after a user's group memberships are updated and they are no longer part of any groups. Developers can use this action to remove the group leader role or perform other cleanup tasks for users who have been removed from all groups. This hook is deprecated in favor of `uo_groups_role_cleanup`.
Usage
add_action( 'uo_groups_role_cleanup', 'your_function_name', 10, 1 );
Parameters
-
$user_id(mixed) - This parameter contains the ID of the user for whom group roles are being cleaned up.
Examples
add_action( 'uo_groups_role_cleanup', 'my_custom_group_role_cleanup', 10, 1 );
/**
* Example function to clean up specific user roles after group membership changes.
*
* This function demonstrates how to hook into the 'uo_groups_role_cleanup'
* action to remove a custom role, like 'group_leader_role', if the user
* is no longer associated with any groups.
*
* @param int $user_id The ID of the user whose group membership has changed.
*/
function my_custom_group_role_cleanup( $user_id ) {
// Define the custom role to be removed.
$custom_role_to_remove = 'group_leader_role';
// Get the user object.
$user = get_user_by( 'id', $user_id );
// Check if the user exists and has the custom role.
if ( $user && $user->has_cap( $custom_role_to_remove ) ) {
// Fetch all groups the user is currently a member of.
// Assuming there's a function like `learndash_get_user_groups` or similar.
// For demonstration, let's simulate this check.
$user_groups = learndash_get_user_groups( $user_id ); // This is a placeholder, replace with actual function.
// If the user is not in any groups (or has been removed from all), remove the role.
if ( empty( $user_groups ) ) {
// Remove the custom role from the user.
$user->remove_role( $custom_role_to_remove );
error_log( "Custom role '{$custom_role_to_remove}' removed from user ID {$user_id} due to no group membership." );
}
}
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/helpers/rest-api-end-points.php:1442
if ( has_action( 'uo_groups_role_cleanup' ) || has_action( 'uo-groups-role-cleanup' ) ) {
do_action_deprecated(
'uo-groups-role-cleanup',
array( $user_id ),
'4.4.1',
'uo_groups_role_cleanup'
); // remove the group leader role if no longer a member of any groups
do_action( 'uo_groups_role_cleanup', $user_id ); // remove the group leader role if no longer a member of any groups
}
SharedFunctions::delete_transient( $user_id );
if ( ! learndash_is_user_in_group( $user_id, $group_id ) ) {
// Remove group leader code if they have used any
$code = ulgm()->group_management->get_user_code( $user_id, $group_id, true );
if ( $code ) {
foreach ( $code as $c ) {