Filter uncanny-toolkit-pro

uo_groupleaderaccess_add_to_group_on_login

Filters whether a user is automatically added to a group upon successful login.

add_filter( 'uo_groupleaderaccess_add_to_group_on_login', $callback, 10, 1 );

Description

Filters whether a user should be automatically added to a group upon login if they have the 'group_leader' role. Allows developers to prevent this automatic addition by returning false. This hook fires after user authentication but before other post-login actions.


Usage

add_filter( 'uo_groupleaderaccess_add_to_group_on_login', 'your_function_name', 10, 1 );

Parameters

$user_id (mixed)
This parameter controls whether the function should proceed with adding the user to a group upon login; `true` allows it, while any other value will prevent it.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the uo_groupleaderaccess_add_to_group_on_login filter.
 * This example conditionally prevents a user from being added to groups on login
 * if they have a specific custom meta key set.
 */
add_filter( 'uo_groupleaderaccess_add_to_group_on_login', function( $should_add, $user_id ) {
    // Check if the user has a custom meta key that indicates they should not be added to groups on login.
    $prevent_auto_enrollment = get_user_meta( $user_id, 'uo_prevent_group_enrollment_on_login', true );

    // If the meta key is set to 'yes', then we should prevent adding to groups.
    if ( 'yes' === $prevent_auto_enrollment ) {
        // Return false to signal that the user should NOT be added to groups.
        return false;
    }

    // Otherwise, return the original value, allowing the default logic to proceed.
    return $should_add;
}, 10, 2 ); // Priority 10, 2 accepted arguments

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/group-leader-access.php:304

public static function add_to_group_on_login( $user_login, $user ) {
		global $wpdb;
		$user_id = $user->ID;

		if( true !== apply_filters('uo_groupleaderaccess_add_to_group_on_login', true, $user_id) ) {
			return; //bailout.
		}
		
		if ( isset( $user->roles ) && is_array( $user->roles ) ) {
			if ( in_array( 'group_leader', $user->roles, true ) ) {
				$results = $wpdb->get_results( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key LIKE '%%%s%%'", $user_id, 'learndash_group_leaders_' ) );
				if ( $results ) {
					foreach ( $results as $result ) {
						$course_access = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", 'learndash_group_enrolled_' . $result->meta_value ) );
						if ( $course_access ) {
							foreach ( $course_access as $c ) {
								ld_update_course_access( $user_id, $c->post_id );
							}
						}
					}
				}
			}
		}
	}

Scroll to Top