Browse: Home / Hooks /

learndash_completion_redirect

apply_filters( 'learndash_completion_redirect',  string $redirect_url,  int $post_id )

Filters URL to redirect to after marking a course complete.


Description #


Parameters #

$redirect_url

(string) Next lesson redirect URL.

$post_id

(int) Post ID.


Source #

File: includes/course/ld-course-progress.php


Examples #

Note: Extended code example below not guaranteed, you may need to consult with a developer

 <?php
/**
 * Example usage for learndash_completion_redirect filter.
 */
add_filter(
	'learndash_completion_redirect',
	function( $link, $post_id ) {

		if ( get_post_type( $post_id ) == 'sfwd-quiz' ) {
			$user_id   = get_current_user_id();
			$course_id = learndash_get_course_id( $post_id );
			if ( ( ! empty( $course_id ) ) && ( ! empty( $user_id ) ) ) {
				// If we have a valid course then we load the course steps.
				$course_steps_object = LDLMS_Factory_Post::course_steps( $course_id );
				$course_steps_object->load_steps();

				// Now we get the linear steps.
				$course_steps_l = $course_steps_object->get_steps( 'l' );
				if ( ! empty( $course_steps_l ) ) {
					// Get the position of the currnet step...
					$current_step_idx = array_search( 'sfwd-quiz:' . $post_id, $course_steps_l );
					if ( false !== $current_step_idx ) {
						// Then check if there are steps after it.
						$next_step_idx = $current_step_idx + 1;
						if ( isset( $course_steps_l[ $next_step_idx ] ) ) {
							// If there are we get the next step id and build a replacement URL.
							list( $next_step_idx, $next_step_id ) = explode( ':', $course_steps_l[ $next_step_idx ] );
							if ( ( ! empty( $next_step_idx ) ) && ( ! empty( $next_step_id ) ) ) {
								$next_step_link = get_permalink( $next_step_id );
								if ( $next_step_link !== $link ) {
									$link = $next_step_link;
								}
							}
						}
					}
				}
			}
		}

		// Always return $link.
		return $link;
	},
	20,
	2
);

add_filter(
	'learndash_completion_redirect',
	function( $link, $post_id ) {

		// We only want to do this for Lessons (sfwd-lessons).
		if ( get_post_type( $post_id ) == 'sfwd-lessons' ) {
			$link = get_permalink( $post_id );
		}

		// Always return $link
		return $link;

	},
	20,
	2
);

add_filter(
	'learndash_completion_redirect',
	function( $link = '', $step_id = 0 ) {
		/**
		 * Baseline check to ensure we have a valid user, link and step_id. Note
		 * $step_id might be a quiz or lesson or topic etc.
		 */
		if ( ( ! is_user_logged_in() ) || ( empty( $link ) ) || ( empty( $step_id ) ) ) {
			return $link;
		}

		// Verify the $step_id is a quiz 'sfwd-quiz' post type.
		$step_post_type = get_post_type( $step_id );
		if ( 'sfwd-quiz' !== $step_post_type ) {
			return $link;
		}

		if ( ( isset( $_GET['quiz_redirect'] ) ) && ( ! empty( $_GET['quiz_redirect'] ) ) ) {
			if ( ( isset( $_GET['quiz_type'] ) ) && ( ! empty( $_GET['quiz_type'] ) ) && ( $_GET['quiz_type'] !== 'global' ) ) {

				$user_id   = get_current_user_id();
				$course_id = learndash_get_course_id( $step_id );

				// Check if the user has passed tis quiz. Just a note we can't determine
				// if they passed just now or if on a previous attempt. All we can really determine
				// is the user passed the quiz at some point.
				$quiz_completed = learndash_is_quiz_complete( $user_id, $step_id, $course_id );
				if ( true === $quiz_completed ) {
					// Here set $link to some alternate URL.

				}
			}
		}

		// Always return $link.
		return $link;
	},
	30,
	2
);

add_filter(
	'learndash_completion_redirect',
	function( $link, $post_id ) {

		// We only want to do this for Topics. But the below code can be adapted to work for Lessons
		if ( get_post_type( $post_id ) == 'sfwd-topic' ) {

			// First we get the topic progress. This will return all the sibling topics.
			// More important it will show the next item
			$progress = learndash_get_course_progress( null, $post_id );

			// Normally when the user completed topic #3 of #5 the 'next' element will point to the #4 topic.
			// But when the student reaches the end of the topic chain it will be empty.
			if ( ! empty( $progress ) && ( isset( $progress['next'] ) ) && ( empty( $progress['next'] ) ) ) {

				// So this is where we now want to get the parent lesson_id and determine if it has a quiz
				$lesson_id = learndash_get_setting( $post_id, 'lesson' );
				if ( ! empty( $lesson_id ) ) {
					$lesson_quizzes = learndash_get_lesson_quiz_list( $lesson_id );
					if ( ! empty( $lesson_quizzes ) ) {

						// If we have some lesson quizzes we loop through these to find the first one not completed by the user.
						// This should be the first one but we don't want to assume.
						foreach ( $lesson_quizzes as $lesson_quiz ) {
							if ( $lesson_quiz['status'] == 'notcompleted' ) {
								// Once we find a non-completed quiz we set the $link to the quiz
								// permalink then break out of out loop
								$link = $lesson_quiz['permalink'];
								break;
							}
						}
					}
				}
			}
		}

		// Always return $link
		return $link;

	},
	20,
	2
);