Filter uncanny-toolkit-pro

ld_lesson_access_from__visible_after_specific_date

Filters the lesson's visibility date, allowing control over when a lesson becomes accessible to a user.

add_filter( 'ld_lesson_access_from__visible_after_specific_date', $callback, 10, 3 );

Description

Filters the date after which a lesson becomes visible for a user. Modify this filter to control lesson visibility based on specific user or lesson data. Primarily used to implement delayed lesson access based on enrollment or completion dates.


Usage

add_filter( 'ld_lesson_access_from__visible_after_specific_date', 'your_function_name', 10, 3 );

Parameters

$visible_after_specific_date (mixed)
This parameter represents the date or boolean value that determines if a lesson should be visible after a specific date.
$lesson_id (mixed)
This parameter represents the date after which the lesson should be visible, if it's set to be visible only after a specific date.
$user_id (mixed)
This parameter represents the ID of the lesson being checked for access.

Return Value

The filtered value.


Examples

add_filter(
	'ld_lesson_access_from__visible_after_specific_date',
	'my_custom_lesson_access_by_date',
	10,
	3
);

/**
 * Custom logic to determine lesson access based on a specific date.
 *
 * This function overrides the default LearnDash behavior for lessons
 * set to be visible only after a specific date.
 *
 * @param mixed $visible_after_specific_date The calculated timestamp for when the lesson becomes visible.
 * @param int   $lesson_id                   The ID of the lesson.
 * @param int   $user_id                     The ID of the user.
 *
 * @return int|null Returns a timestamp if the lesson should be accessible, otherwise null.
 */
function my_custom_lesson_access_by_date( $visible_after_specific_date, $lesson_id, $user_id ) {
	// Get the current time as a Unix timestamp.
	$current_time = time();

	// If the calculated 'visible_after_specific_date' is in the past,
	// the lesson is already accessible based on the default LearnDash settings.
	// We can return the provided value which will indicate accessibility.
	if ( $current_time >= $visible_after_specific_date ) {
		return $visible_after_specific_date;
	}

	// Example: Let's say we want to manually restrict access for a specific lesson (e.g., lesson ID 123)
	// for all users until a later specific date, overriding the default.
	$manual_override_lesson_id = 123;
	$manual_override_end_timestamp = strtotime( '2025-01-01 00:00:00' ); // Example: Override until January 1st, 2025.

	if ( $lesson_id === $manual_override_lesson_id && $current_time < $manual_override_end_timestamp ) {
		// If it's the manually overridden lesson and the current time is before our override end date,
		// return the override end timestamp. This effectively makes the lesson inaccessible until then.
		return $manual_override_end_timestamp;
	}

	// If no custom logic applies or the conditions for custom logic are not met,
	// return the original $visible_after_specific_date value.
	// This will be used by LearnDash to check if the current time is before this date.
	return $visible_after_specific_date;
}

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/uncanny-drip-lessons-by-group.php:734

public static function ld_lesson_access_from_inherited_from_ld( $lesson_id, $user_id, $course_id = null ) {
		$return = null;

		if ( is_null( $course_id ) ) {
			$course_id = learndash_get_course_id( $lesson_id );
		}

		$courses_access_from = ld_course_access_from( $course_id, $user_id );
		if ( empty( $courses_access_from ) ) {
			$courses_access_from = learndash_user_group_enrolled_to_course_from( $user_id, $course_id );
		}

		$visible_after = learndash_get_setting( $lesson_id, 'visible_after' );
		if ( $visible_after > 0 ) {

			// Adjust the Course acces from by the number of days. Use abs() to ensure no negative days.
			$lesson_access_from = $courses_access_from + abs( $visible_after ) * 24 * 60 * 60;
			$lesson_access_from = apply_filters( 'ld_lesson_access_from__visible_after', $lesson_access_from, $lesson_id, $user_id );

			$current_timestamp = time();
			if ( $current_timestamp < $lesson_access_from ) {
				$return = $lesson_access_from;
			}

		} else {
			$visible_after_specific_date = learndash_get_setting( $lesson_id, 'visible_after_specific_date' );
			if ( ! empty( $visible_after_specific_date ) ) {
				if ( ! is_numeric( $visible_after_specific_date ) ) {
					// If we a non-numberic value like a date stamp Y-m-d hh:mm:ss we want to convert it to a GMT timestamp
					$visible_after_specific_date = learndash_get_timestamp_from_date_string( $visible_after_specific_date, true );
				}

				$current_time = time();

				if ( $current_time < $visible_after_specific_date ) {
					$return = apply_filters( 'ld_lesson_access_from__visible_after_specific_date', $visible_after_specific_date, $lesson_id, $user_id );
				}
			}
		}

		return $return;
	}


Scroll to Top