Filter uncanny-learndash-groups

ulgm_learndash_group_enrolled_users

Filters the data for users enrolled in a LearnDash group, allowing modification before display.

add_filter( 'ulgm_learndash_group_enrolled_users', $callback, 10, 4 );

Description

Fires after retrieving enrolled users for a LearnDash group, allowing developers to modify the user data array before it's returned. Useful for adding custom fields, filtering specific users, or altering display logic. The `$data` parameter holds the user array, `$group_id` the current group's ID, `$args` the query arguments, and `$user_id` the current user's ID.


Usage

add_filter( 'ulgm_learndash_group_enrolled_users', 'your_function_name', 10, 4 );

Parameters

$data (mixed)
This parameter contains the data array of enrolled users for the group.
$group_id (mixed)
This parameter contains the data being filtered, which represents the list of users enrolled in the group.
$args (mixed)
This parameter represents the unique identifier for the LearnDash group.
$user_id (mixed)
This parameter contains an array of arguments passed to the function, which can be used to further filter or modify the output.

Return Value

The filtered value.


Examples

add_filter( 'ulgm_learndash_group_enrolled_users', 'my_custom_group_enrolled_users_filter', 10, 4 );

/**
 * Custom filter to modify the list of enrolled users for a LearnDash group.
 *
 * This example demonstrates how to:
 * 1. Check if the current user is an administrator.
 * 2. If it is, append a specific user (e.g., a manager) to the enrolled users list.
 * 3. Otherwise, return the original data.
 *
 * @param array $data     The array of enrolled user data for the group.
 * @param int   $group_id The ID of the LearnDash group.
 * @param array $args     Additional arguments passed to the function.
 * @param int   $user_id  The ID of the user for whom the enrolled users are being retrieved.
 *
 * @return array The modified or original array of enrolled user data.
 */
function my_custom_group_enrolled_users_filter( $data, $group_id, $args, $user_id ) {
    // Check if the current user has administrator privileges.
    if ( current_user_can( 'manage_options' ) ) {
        // Define the ID of the user you want to always show as enrolled (e.g., a group manager).
        $manager_user_id = 123; // Replace with the actual user ID.

        // Check if the manager is not already in the data to avoid duplicates.
        $manager_already_enrolled = false;
        if ( ! empty( $data ) ) {
            foreach ( $data as $user_data ) {
                if ( $user_data['user_id'] == $manager_user_id ) {
                    $manager_already_enrolled = true;
                    break;
                }
            }
        }

        // If the manager is not already enrolled, add them.
        if ( ! $manager_already_enrolled ) {
            // Fetch basic user data for the manager. In a real scenario, you might fetch more if needed.
            $manager_user_info = get_user_meta( $manager_user_id, '', true );
            $manager_display_name = get_user_meta( $manager_user_id, 'display_name', true ) ?: get_userdata( $manager_user_id )->user_login;

            $data[] = array(
                'user_id'      => $manager_user_id,
                'display_name' => $manager_display_name,
                'user_login'   => get_userdata( $manager_user_id )->user_login,
                // Add any other relevant user data that matches the expected format of $data.
            );
        }
    }

    // Return the potentially modified data.
    return $data;
}

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/learndash/learndash-function-overrides.php:824

}

		/*
		 * Filter for customizing group users
		 * ulgm_learndash_group_enrolled_users
		 */

		$data = apply_filters( 'ulgm_learndash_group_enrolled_users', $data, $group_id, $args, $user_id );

		return $data;
	}
}

Scroll to Top