learndash_previous_step_completed
Filters whether the previous step in a LearnDash course has been completed by the user.
add_filter( 'learndash_previous_step_completed', $callback, 10, 1 );
Description
Fires to determine if the "previous step completed" check should prevent progression. Developers can filter `false` to override the default behavior, allowing progression even if the previous lesson/topic isn't marked as completed. Useful for custom progression logic.
Usage
add_filter( 'learndash_previous_step_completed', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example of how to use the 'learndash_previous_step_completed' filter hook.
* This filter allows you to modify the logic for determining if the previous step is considered completed.
* In this example, we'll assume a scenario where a custom completion status needs to be checked.
*
* @param bool $previous_step_completed The current status of whether the previous step is considered completed.
* @param int $prev_step_id The ID of the previous step (lesson or topic).
* @param int $user_id The ID of the user.
* @return bool The modified status of whether the previous step is completed.
*/
add_filter(
'learndash_previous_step_completed',
function( $previous_step_completed, $prev_step_id, $user_id ) {
// In a real scenario, you might have custom logic here to check
// for a specific completion status that isn't the default 'completed'.
// For demonstration, let's imagine we have a custom meta key
// that needs to be checked for advanced completion.
$custom_completion_meta_key = '_my_advanced_completion_status';
$custom_completion_value = 'fully_mastered';
// Get the meta data for the previous step
$meta_value = get_post_meta( $prev_step_id, $custom_completion_meta_key, true );
// If the custom completion status is met for this user,
// consider the previous step as completed, even if LearnDash's
// default 'completed' status is not yet set (or is being overridden).
if ( ! empty( $meta_value ) && $meta_value === $custom_completion_value ) {
// Override the default $previous_step_completed to true
return true;
}
// If our custom logic doesn't apply, return the original value passed to the filter.
return $previous_step_completed;
},
10, // Priority
3 // Number of accepted arguments
);
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/lesson-topic-auto-complete.php:627
public static function learndash_show_next_link_progression( $show_next_link = false, $user_id = 0, $post_id = 0 ) {
$post = get_post( $post_id );
if ( 'sfwd-lessons' === $post->post_type ) {
$progress = learndash_get_course_progress( null, $post->ID );
if ( ! empty( $progress['prev'] ) && empty( $progress['prev']->completed ) && learndash_lesson_progression_enabled() ) {
return false;
}
}
if ( 'sfwd-topic' === $post->post_type ) {
$progress = learndash_get_course_progress( null, $post->ID );
if ( ! empty( $progress['prev'] ) && empty( $progress['prev']->completed ) && learndash_lesson_progression_enabled() ) {
if ( ! apply_filters( 'learndash_previous_step_completed', false, $progress['prev']->ID, $user_id ) ) {
return false;
}
}
}
// If the module is enabled, but autocomplete is disabled globally (or on an individual lesson settings page), Revert to LearnDash navigation?
$feature_auto_complete_default = self::get_settings_value( 'uo_global_auto_complete', 'uncanny_pro_toolkitLessonTopicAutoComplete' );
$revert_next_button = false;
if( 'auto_complete_only_lesson_topics_set' === $feature_auto_complete_default ) {
$revert_next_button = true; // Auto complete is disabled globally.
}
$post_options_auto_complete = (array) learndash_get_setting( $post );
if ( isset( $post_options_auto_complete['uo_auto_complete'] ) ) {
if ( 'disabled' === $post_options_auto_complete['uo_auto_complete'] ) { // Is Auto complete disable for the post?
$revert_next_button = true;
}
}
if( $revert_next_button ) {
return $show_next_link; // return the LD default value.
}
return true;
}