Dealing with end-of-course redirect
-
I have wanted to re-direct folks after the last lesson in a course to the course overview page. This has turned out to be more complicated than I thought, so here is a short writeup on how I did it. This requires adding custom functions in your functions.php file and I use a “meta” tag on the final lesson to indicate where I want to jump to after the lesson has been marked complete. It would be really, really nice if I didn’t have to go through all of this to do something this simple.
1) use the action ‘lifterlms_after_mark_complete_lesson’ to add a small script after the form that changes the post variable “mark-complete”, I change it to ‘custom-mark-complete’ by injecting the following script:
add_action('lifterlms_after_mark_complete_lesson', 'customizeForm'); function customizeForm() { echo '<script> formtype = document.getElementsByName("mark-complete"); formtype[0].setAttribute("name", "custom-mark-complete"); </script>'; return; }
2) then, I copied your entire mark-complete function into my own functions.php file, change the name and add it as an action that runs before your current mark-complete function: I make a few changes..
– I check if it is a custom-mark-complete first to avoid triggering the nonce
– After the lesson has been marked complete, I add a check for a meta value associated with the lesson. I’ve defined the meta value as redirectAfterMarkComplete which will hold the value of where I want to redirect as a URL.
5) if this meta value exists, it overrides all other processing and jumps there.
this fixes the basic problem for the case where the next-lesson doesn’t exist.
a far better solution would be for you to define a hook for the case where next-lesson doesn’t existadd_action( 'init', 'custom_mark_complete' ,5 ); // run this before the regular mark_complete function custom_mark_complete() { $request_method = strtoupper( getenv( 'REQUEST_METHOD' ) ); if ( 'POST' !== $request_method ) { return; } // required fields if ( ! isset( $_POST['custom-mark-complete'] ) ) { return; } // verify nonce if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'mark_complete' ) ) { return; } $lesson_id = absint( $_POST['custom-mark-complete'] ); if ( ! $lesson_id || ! is_numeric( $lesson_id ) ) { llms_add_notice( __( 'An error occurred, please try again.', 'lifterlms' ), 'error' ); } else { if ( llms_mark_complete( get_current_user_id(), $lesson_id, 'lesson', 'lesson_' . $lesson_id ) ) { llms_add_notice( sprintf( __( 'Congratulations! You have completed %s', 'lifterlms' ), get_the_title( $lesson_id ) ) ); if ( apply_filters( 'lifterlms_autoadvance', true ) ) { $redirectValue = get_post_meta( $lesson_id, 'redirectAfterMarkComplete', true ); // this checks to see if there is a special redirect defined if ($redirectValue) { // if so, go there! wp_redirect( $redirectValue); exit; } $lesson = new LLMS_Lesson( $lesson_id ); // else go to next lesson as planned $next_lesson_id = $lesson->get_next_lesson(); if ( $next_lesson_id ) { wp_redirect( apply_filters( 'llms_lesson_complete_redirect', get_permalink( $next_lesson_id ) ) ); exit; } } } } }
- The topic ‘Dealing with end-of-course redirect’ is closed to new replies.