learndash_completion_redirect
This filter is documented in includes/course/ld-course-progress.php */ Filters the redirect URL after a user completes a course or lesson.
add_filter( 'learndash_completion_redirect', $callback, 10, 1 );
Description
Filters the redirect link after a lesson or topic is marked complete. Developers can modify the `$link` to point to a custom URL or conditionally change the redirection destination. This hook fires after the next lesson or topic is determined but before the redirect occurs.
Usage
add_filter( 'learndash_completion_redirect', 'your_function_name', 10, 1 );
Parameters
-
$link(mixed) - - **$post** `mixed`
Return Value
The filtered value.
Examples
/**
* Example of using the 'learndash_completion_redirect' filter to
* conditionally redirect users to a different page after completing a lesson or topic.
*
* This example checks if the user has completed a "Special Module" and, if so,
* redirects them to a custom completion page instead of the default next lesson/topic.
*
* @param string|WP_Error $link The default redirect link provided by LearnDash.
* @param int $post_id The ID of the completed post (lesson, topic, quiz).
* @return string|WP_Error The modified or original redirect link.
*/
add_filter( 'learndash_completion_redirect', function( $link, $post_id ) {
// Ensure we are dealing with a valid post ID.
if ( ! $post_id || is_wp_error( $post_id ) ) {
return $link;
}
// Get the post object for the completed item.
$completed_post = get_post( $post_id );
// Check if the completed post is a lesson.
if ( $completed_post && 'sfwd-lessons' === $completed_post->post_type ) {
// Get the course ID for the lesson.
$course_id = learndash_get_course_id( $post_id );
// Check if the lesson belongs to a specific course that requires special handling.
// Replace 'YOUR_SPECIAL_COURSE_ID' with the actual ID of your special course.
if ( $course_id === YOUR_SPECIAL_COURSE_ID ) {
// Check if the user has a specific meta key indicating they should go to the special page.
// Replace 'has_completed_special_module' with your actual meta key.
if ( get_user_meta( get_current_user_id(), 'has_completed_special_module', true ) ) {
// Redirect to a custom completion page for this special module.
// Replace '/your-special-completion-page/' with the actual URL.
return '/your-special-completion-page/';
}
}
}
// If no special conditions are met, return the original link.
return $link;
}, 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/mark-lessons-complete.php:461
protected static function learndash_get_next_lesson_redirect( $post = null ) {
if ( empty( $post->ID ) ) {
global $post;
}
$next = learndash_next_post_link( '', true, $post );
if ( ! empty( $next ) ) {
$link = $next;
} else {
if ( 'sfwd-topic' === $post->post_type ) {
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) == 'yes' ) {
$course_id = learndash_get_course_id( $post->ID );
$lesson_id = learndash_course_get_single_parent_step( $course_id, $post->ID );
} else {
$lesson_id = learndash_get_setting( $post, 'lesson' );
}
$link = get_permalink( $lesson_id );
} else {
$course_id = learndash_get_course_id( $post );
$link = self::learndash_next_global_quiz( true, null, $course_id );
}
}
if ( ! empty( $link ) ) {
/** This filter is documented in includes/course/ld-course-progress.php */
$link = apply_filters( 'learndash_completion_redirect', $link, $post->ID );
if ( ! empty( $link ) ) {
learndash_safe_redirect( $link );
}
}
return '';
}