Learndash How to drip lesson's After watching the lesson ant not based enrollment

33 Views Asked by At

On Learndash, I want the next lesson to be available only 3 days after finishing watching the previous lesson.

I am training this code, but it won't work. Can someone help?

// Add a custom action after a lesson is completed
add_action('learndash_lesson_complete', 'custom_delayed_lesson_access', 10, 2);

function custom_delayed_lesson_access($user_id, $lesson_id) {
    // Set the waiting period in seconds (3 days in this example)
    $waiting_period = 3 * 24 * 60 * 60;

    // Calculate the timestamp when the next lesson should be accessible
    $next_lesson_access_time = time() + $waiting_period;

    // Update user meta with the timestamp for the next lesson access
    update_user_meta($user_id, 'next_lesson_access_' . $lesson_id, $next_lesson_access_time);
}

// Filter the lesson access time
add_filter('learndash_lesson_access_from', 'custom_filter_lesson_access', 10, 2);

function custom_filter_lesson_access($access_from, $lesson_id) {
    // Get the user ID
    $user_id = get_current_user_id();

    // Get the timestamp for the next lesson access
    $next_lesson_access_time = get_user_meta($user_id, 'next_lesson_access_' . $lesson_id, true);

    // If the timestamp is set, use it as the new lesson access time
    if (!empty($next_lesson_access_time)) {
        return $next_lesson_access_time;
    }

    // Otherwise, use the default access time
    return $access_from;
}
0

There are 0 best solutions below