Does your LearnDash site feature multiple course selections? When a new user registers on your site do you wish for them to be enrolled in more than one course? Below we will provide an example of how to enroll a new user into multiple courses on registration.
Getting Started #
Our documentation will require PHP and/or JavaScript code. To use the code you must place it in your WordPress code in an appropriate place to load, usually in a theme or plugin.
PHP #
PHP code can be placed in your theme’s functions.php file, or in a standalone plugin if you know how to make one.
JavaScript #
JavaScript code should be placed in a JavaScript file (.js) and enqueued using wp_enqueue_scripts. There are other ways of getting JavaScript on the page, but this is the most recommended to avoid future problems with JavaScript code on the page.
Example #
add_action( 'user_register', function( $user_id ) {
// Comma separated array of course IDs
// Go here if you don't know how to find Course ID: https://www.learndash.com/support/docs/faqs/find-course-id/
$course_ids = [
27922,
27949,
];
// Add user to each course
foreach ( $course_ids as $course_id ) {
ld_update_course_access( $user_id, $course_id, $remove = false );
}
});
The callback function, ld_update_course_access should return a URL (string) of where to send the user.