learndash_course_completion_url
This filter is documented in includes/course/ld-course-functions.php */ Filters the URL a user is redirected to after completing a LearnDash course.
add_filter( 'learndash_course_completion_url', $callback, 10, 1 );
Description
Filters the URL used after a course is completed. Developers can change the redirection URL. Defaults to the course permalink.
Usage
add_filter( 'learndash_course_completion_url', 'your_function_name', 10, 1 );
Parameters
-
$return(mixed) - - **$id** `mixed`
Return Value
The filtered value.
Examples
/**
* Example function to modify the course completion URL.
* This function can be used to redirect users to a specific page or a custom URL
* after they complete a LearnDash course.
*
* @param string $return The default course completion URL.
* @param int $course_id The ID of the course that was completed.
* @param int $user_id The ID of the user who completed the course.
*
* @return string The modified course completion URL.
*/
function my_custom_learndash_course_completion_url( $return, $course_id, $user_id ) {
// Check if the user has actually completed the course (this is a simplified check for example)
// In a real scenario, you'd likely have a more robust check here, perhaps using
// learndash_is_course_completed( $user_id, $course_id ).
if ( learndash_is_course_completed( $user_id, $course_id ) ) {
// Example: Redirect to a custom thank you page for this specific course.
// You could dynamically determine the URL based on course ID or user meta.
$custom_thank_you_url = '/my-course-completion-page/?course_id=' . $course_id;
// You can also perform other actions here, like sending an email,
// granting a certificate, or updating user meta.
return home_url( $custom_thank_you_url );
}
// If the course isn't considered completed by our logic, return the original URL.
return $return;
}
add_filter( 'learndash_course_completion_url', 'my_custom_learndash_course_completion_url', 10, 3 );
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:512
protected static function learndash_next_global_quiz( $url = true, $user_id = null, $id = null, $exclude = [] ) {
if ( empty( $id ) ) {
$id = learndash_get_course_id();
}
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$quizzes = learndash_get_global_quiz_list( $id );
$return = get_permalink( $id );
if ( ! empty( $quizzes ) ) {
foreach ( $quizzes as $quiz ) {
if ( ! in_array( $quiz->ID, $exclude, true ) && learndash_is_quiz_notcomplete( $user_id, [ $quiz->ID => 1 ], false, $id ) && learndash_can_attempt_again( $user_id, $quiz->ID ) ) {
if ( $url ) {
return get_permalink( $quiz->ID );
} else {
return $quiz->ID;
}
}
}
}
// Good to know:
// Filter name `learndash_course_completion_url` does not seem correct in the context of this function.
// But it will stay here for backward compatibility.
// It is moved to the correct place in version TBD together with parameters updating.
/** This filter is documented in includes/course/ld-course-functions.php */
$return = apply_filters(
'learndash_course_completion_url',
Cast::to_string( $return ),
Cast::to_int( $id ),
0
);
return $return;
}