Browse: Home / Snippets /

Enroll new registered user into course and group

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

/**
 * LearnDash - Example kook into WordPress 'register_new_user' action 
 * to enroll user into a course or group.
 */
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 );
			}
		}
	}
} );