One of the features I recently finished for the my redesign of my JavaScript for WordPress site was to have users taken to a special page once they complete a course.
You can do this a number of ways, but here is how I did it:
1. Setup Congratulations Pages for Each Course
The first thing I did was create a parent page called Congratulations with the url of “/congrats.”
Then I created Child Pages, one for each course. Those pages looked something like this.
You can see the badge displayed and the number of points earned (I use BadgeOS and BadgeOS LearnDash Add-on).
The most important thing I did was give each page the same slug as the course it is tied to. If I didn’t do this, then the next step wouldn’t work as coded.
2. Add This Code to Your Functions.php
The next step was to find the LearnDash hook that fires when a LearnDash course completes.
Luckily there is a learndash_course_completion_url hook that let’s return a URL and the redirect happens automatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function custom_course_completed_redirect($link, $course_id) { | |
$link = get_post_field( 'post_name', $course_id ); | |
//You can change the link here | |
return '/congrats/' . $link; | |
} | |
add_filter("learndash_course_completion_url", custom_course_completed_redirect, 5, 2); | |
?> |
This code gets the slug for the completed course and then creates a new URL string of “/congrats/course-slug.”
NOTE: This will only work if you you did these two things from Step 1.
- Create a parent page with the URL slug of “congrats”
- Create child pages for each course using the same URL slug as the course
Hope this Helps!
If you have a LearnDash account you can access the list of Action and Filter Hooks here. Since that page is password protected, thought I would share this snippet with you.
Hope it helps!
Hello Zac,
Can this snippet work with a Custom post type instead of the page?
Please advise, thanks.
Kendell
LikeLike
Hey Kendell!
The reason this works is because LearnDash has a hook in place for you to use.
To do this with a custom post type you would need to add a bit more code, specifically a button or something that a user would click that calls a function similar to this one.
Hope that helps point you towards a solution!
LikeLiked by 1 person
Hiyo Zac, I think
add_filter(“learndash_course_completion_url”, custom_course_completed_redirect, 5, 2);
should be
add_filter(“learndash_course_completion_url”, “custom_course_completed_redirect”, 5, 2);
LikeLike
Hi, is it also possible to redirect someone to a specific page after they mark a ‘Lesson’ as complete? Let me know!
LikeLike
Probably! I believe there is a hook for this! Ask the LearnDash support team over on their site and they’ll help you out!
LikeLike
Hi Zac, I just want the users to be directed to one particular page after each course.
That page is /missionsafslutning/
I want the users to be directed to that page after every course.
What would your code snippet look like then?
LikeLike
You would just change this:
return ‘/congrats/’ . $link;
To this:
return ‘/missionsafslutning/’;
LikeLike
I tried insert this code into my theme’s functions.php but then WordPres gives the following error:
CODE
function custom_course_completed_redirect($link, $course_id) {
$link = get_post_field( ‘post_name’, $course_id );
//You can change the link here
return ‘/missionsafslutning/’;
}
add_filter(“learndash_course_completion_url”, custom_course_completed_redirect, 5, 2);
ERROR
constant custom_course_completed_redirect – assumed ‘custom_course_completed_redirect’ (this will throw an Error in a future version of PHP) in /var/www/missionkonstantin.dk/public_html/wp-content/themes/power-magazine/functions.php on line 226
Warning: Cannot modify header information – headers already sent by (output started at /var/www/missionkonstantin.dk/public_html/wp-content/themes/power-magazine/functions.php:226) in /var/www/missionkonstantin.dk/public_html/wp-includes/functions.php on line 6029
Warning: Cannot modify header information – headers already sent by (output started at /var/www/missionkonstantin.dk/public_html/wp-content/themes/power-magazine/functions.php:226) in /var/www/missionkonstantin.dk/public_html/wp-admin/includes/misc.php on line 125
LikeLike