Filter tin-canny-learndash-reporting

uotc_ld_allowed_roles

Filters the array of user roles allowed to access LearnDash features, enabling custom role management.

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

Description

This filter allows developers to modify the list of user roles permitted to view LearnDash group quiz reports. By default, only administrators and group leaders have access. Developers can extend or restrict this access by returning a custom array of role slugs.


Usage

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

Return Value

The filtered value.


Examples

// Example of how to add a filter to 'uotc_ld_allowed_roles'
// This will add 'course_manager' to the list of allowed roles for accessing the group quiz report.
add_filter( 'uotc_ld_allowed_roles', function( $allowed_roles ) {

	// Add 'course_manager' role if it's not already present
	if ( ! in_array( 'course_manager', $allowed_roles, true ) ) {
		$allowed_roles[] = 'course_manager';
	}

	// If you wanted to remove a role, you could do something like this:
	// $allowed_roles = array_diff( $allowed_roles, array( 'group_leader' ) );

	return $allowed_roles;
}, 10, 1 ); // Priority 10, accepts 1 argument

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/reporting/learndash/frontend-reports/group-quiz-report.php:1197

private function user_access_role( $user ) {

		if ( ! $user instanceof WP_User ) {
			return false;
		}

		// Allow role to be filtered
		$allowed_roles = apply_filters(
			'uotc_ld_allowed_roles',
			array(
				'administrator',
				'group_leader',
			)
		);

		// Check if the user is an administrator
		if ( in_array( 'administrator', $user->roles, true ) ) {
			return 'administrator';
		}

		// Check if the user is a group leader
		if ( in_array( 'group_leader', $user->roles, true ) ) {
			return 'group_leader';
		}

		// Check if user has allowed role
		foreach ( $allowed_roles as $role ) {
			if ( in_array( $role, $user->roles, true ) ) {
				return 'administrator'; // Return admin role for custom roles.
			}
		}

		return false;
	}

Scroll to Top