Filter uncanny-learndash-groups

ulgm_learndash_group_enrolled_courses

Filters the list of courses a group is enrolled in, allowing modifications before the list is displayed.

add_filter( 'ulgm_learndash_group_enrolled_courses', $callback, 10, 2 );

Description

Filters the list of courses a LearnDash group is enrolled in. Developers can modify the array of course IDs before they are returned. This hook is useful for programmatically adding or removing courses from group enrollments.


Usage

add_filter( 'ulgm_learndash_group_enrolled_courses', 'your_function_name', 10, 2 );

Parameters

$results (mixed)
This parameter contains the results of enrolled courses for a group, which can be filtered by other plugins or themes.
$group_id (mixed)
This parameter contains an array of course IDs that the user is enrolled in within the specified group.

Return Value

The filtered value.


Examples

/**
 * Example of using the ulgm_learndash_group_enrolled_courses filter.
 * This example adds a specific course to the enrolled courses for a group
 * if the group ID is 50.
 */
add_filter( 'ulgm_learndash_group_enrolled_courses', function( $enrolled_courses, $group_id ) {

    // Only modify for a specific group, e.g., group ID 50
    if ( 50 === $group_id ) {
        // Define a course ID to add. Replace '123' with an actual Course ID.
        $course_to_add = 123;

        // Ensure the course ID is an integer
        $course_to_add = absint( $course_to_add );

        // If the course ID is valid and not already in the list, add it.
        if ( $course_to_add > 0 && ! in_array( $course_to_add, $enrolled_courses ) ) {
            $enrolled_courses[] = $course_to_add;
        }
    }

    // Always return the modified (or original) array of enrolled courses.
    return $enrolled_courses;

}, 10, 2 ); // 10 is the priority, 2 is the number of arguments the callback function accepts

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:387

public static function learndash_group_enrolled_courses( $group_id = 0, $bypass_transient = false, $disable_hierarchy = false ) {

		// For group hierarchy support
		$is_hierarchy_setting_enabled = false;
		if ( function_exists( 'learndash_is_groups_hierarchical_enabled' ) && learndash_is_groups_hierarchical_enabled() && 'yes' === get_option( 'ld_hierarchy_settings_child_groups', 'no' ) ) {
			$is_hierarchy_setting_enabled = true;
		}

		if ( $disable_hierarchy ) {
			$is_hierarchy_setting_enabled = false;
		}

		$is_hierarchy_setting_enabled = apply_filters(
			'ulgm_is_hierarchy_setting_enabled',
			$is_hierarchy_setting_enabled,
			$group_id,
			$bypass_transient,
			$disable_hierarchy
		);

		$group_id = absint( $group_id );
		// Bail early if group id is not set
		if ( 0 === $group_id || empty( $group_id ) || is_null( $group_id ) ) {
			return array();
		}

		// check if there's transient data available
		if ( false === $bypass_transient ) {
			$transient_key = 'learndash_group_enrolled_courses_' . $group_id;
			if ( $is_hierarchy_setting_enabled ) {
				$transient_key .= '_hierarchy';
			}
			$group_users_objects = self::get_ld_transient( $transient_key );
			if ( ! empty( $group_users_objects ) ) {
				return $group_users_objects;
			}
		}

		$search_condition = " meta_key = 'learndash_group_enrolled_{$group_id}' ";
		if ( $is_hierarchy_setting_enabled ) {
			$group_children = learndash_get_group_children( $group_id );
			if ( ! empty( $group_children ) ) {
				foreach ( $group_children as $child_group_id ) {
					$child_group_id   = absint( $child_group_id );
					$search_condition .= " OR meta_key = 'learndash_group_enrolled_{$child_group_id}' ";
				}
			}
		}

		global $wpdb;

		$qry = "SELECT pm.post_id FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts p ON p.ID = pm.post_id WHERE 1=1 AND p.post_status = 'publish' AND ( $search_condition ) ";

		$results = $wpdb->get_col( $qry );

		if ( empty( $results ) ) {
			$results = array();
		}

		if ( ! empty( $results ) ) {
			$results = array_values( array_unique( $results ) );
		}

		/*
		 * Filter for customizing group courses
		 * ulgm_learndash_group_enrolled_courses
		 */

		$results = apply_filters( 'ulgm_learndash_group_enrolled_courses', $results, $group_id );

		if ( false === $bypass_transient ) {
			self::set_ld_transient( $transient_key, $results );
		}

		return $results;
	}

Scroll to Top