Filter uncanny-toolkit-pro

uo_ld_expire_group_remove_courses

Filters whether to remove enrolled courses from a group when its expiration date is reached.

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

Description

Filters whether to automatically remove enrolled courses when a LearnDash group expires. By default, this is true. Developers can return false to prevent course removal or modify the courses to be removed. This hook fires during the group expiration process.


Usage

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

Parameters

$post_id (mixed)
This parameter determines whether the group's enrolled courses should be removed.

Return Value

The filtered value.


Examples

/**
 * Example of using the 'uo_ld_expire_group_remove_courses' filter.
 *
 * This filter allows developers to conditionally prevent the removal of courses
 * from a LearnDash group when the group expires.
 *
 * @param bool $remove_courses Whether to proceed with removing courses. Defaults to true.
 * @param int  $group_id The ID of the group that is expiring.
 * @return bool True to remove courses, false to prevent removal.
 */
add_filter( 'uo_ld_expire_group_remove_courses', function( $remove_courses, $group_id ) {
	// Prevent removal of courses if the group has a specific meta key indicating it should not be cleared.
	if ( get_post_meta( $group_id, 'uo_keep_group_courses_after_expiration', true ) ) {
		// Return false to stop the default behavior of removing courses from the group.
		return false;
	}

	// Otherwise, allow the default behavior to proceed.
	return $remove_courses;
}, 10, 2 );

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/learn-dash-group-expiration.php:734

public static function expire_group( $post_id ) {
		if ( true === apply_filters( 'uo_ld_expire_group_remove_courses', true, $post_id ) ) {
			if ( function_exists( 'learndash_group_enrolled_courses' ) ) {
				$group_enrolled_courses = learndash_group_enrolled_courses( $post_id, true );

				if ( empty( $group_enrolled_courses ) ) {
					wp_clear_scheduled_hook( 'uo-expire-group', array( $post_id ) );
					wp_clear_scheduled_hook( 'uo-email-group', array( $post_id ) );

					return;
				}

				foreach ( $group_enrolled_courses as $course_id ) {
					ld_update_course_group_access( $course_id, $post_id, true );
				}
			}
		}
		update_post_meta( $post_id, 'uo-expiration-date-expired', current_time( get_option( 'date_format' ) . ' H:i:s' ) );

		wp_clear_scheduled_hook( 'uo-expire-group', array( $post_id ) );
		wp_clear_scheduled_hook( 'uo-email-group', array( $post_id ) );
	}

Scroll to Top