How to redirect to a page after completing a Learndash course
How to redirect to a page after completing a Learndash course
This tutorial is an improve version of Zac Gordon’s How to Redirect to Custom Page After Completing a LearnDash Course version.
While his tutorial works just fine, it is limited only for Linear Course progression setting.
What is similar?
Like Zac’s tutorial, in order for this to work you must do the following:
- First, create a parent page and name it to anything you like. mine will be name as “Congrats”.
- Then create a child page under “Congrats” with the same slug URL of the course you want to be redirected.
- Lastly, add a code snippet on functions.php (recommended in a child theme)
Optionally but recommended, make the Parent page private. As it has no content, it’s better to be unavailable for public view.
What is different between Zac’s and mine?
With Zac’s code, it is more incline for Linear courses. Meaning, you can’t jump from lesson to lesson or topic to topic. Everything is in order. If however, the author wanted to make his course Free form, his code doesn’t work anymore. Every time you click the last topic, it will redirect you to the page you set. Regardless if all topics and lessons are completed.
The main difference is the code snippet itself. In his code, he actually use LearnDash’s filter hook learndash_course_completion_url that filters the URL. This make sense why it redirects after the last topic has been completed even if the course is not yet totally completed at all.
With my improve code, we are actually using the correct action hook that will redirect any courses that is truly completed.
The Code snippet explained
[php] add_action('learndash_course_completed', 'course_completed', 10, 1); function course_completed( $data ) { $course_id = $data['course']->ID; $slug_url = get_post_field( 'post_name', $course_id ); if ($course_id) { wp_redirect(site_url("/congrats/$slug_url")); exit; } } [/php]
Using the action hook learndash_course_completed, it triggers a function right after a course has been completed. Instead of filtering a URL like what Zac is actually doing.
So in my code, we created a trigger using the action hook, then get the course URL and redirect it right away to the child page.
Hopefully, I have explained it well. Happy to help everyone! 🙂
Recent Comments