Enroll newly registered users into a course and group in LearnDash LMS

Enroll newly registered users into a course and group in LearnDash LMS on child sites.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'register_new_user', function( $user_id = 0 ) {
    if ( ! empty( $user_id ) ) {
        // Do something with the new user ID. 
         
        //Maybe call the LD function to enroll them into a course.  
        $course_id = 123; // Dummy course ID for new registrations.
        ld_update_course_access( $user_id, $course_id );
 
        // Or add them to a Group
        $group_id = 456; // Dummy group ID for new registrations.
        ld_update_group_access( $user_id, $group_id );
 
        /**
        * Lastly, I'm making an assumption here that since the call is being made into the site 
        * via a URL the URL parameters might be available as well. So assume you are able to pass 
        * in via the URL some values like 'course_id=123&group_id=456' this can be very dynamic. 
        * For example:
        */
        if ( isset( $_GET['course_id'] ) ) {
            $course_id = absint( $_GET['course_id'] );
            if ( ! empty( $course_id ) ) {
                ld_update_course_access( $user_id, $course_id );
            }
        }
 
        if ( isset( $_GET['group_id'] ) ) {
            $group_id = absint( $_GET['group_id'] );
            if ( ! empty( $group_id ) ) {
                ld_update_group_access( $user_id, $group_id );
            }
        }
    }
} );