Adding newly registered users to different leardash groups depending on different wordpress registration page

286 Views Asked by At

I have two WordPress registration pages eg https://example.com/adult-register-url and https://example.com/youth-register-url. I have created 2 learndash groups with different courses one group is the adult group and the other group is the youth group. I am now trying to automatically add users that register with the adult-register-url to the adult group and the youth-register-url to the youth group. However, on registration users are not being added to the correct group. Below is the code that I added to the functions.php

add_action( 'user_register', function ( $user_id = 0 ){
        
    global $post;
    
        $current_slug = $post->name;
        
    
    if(isset($current_slug) and $current_slug =='youth-register-url'){
        if ( !empty( $user_id ) ) {
        $LD_GROUP_IDS = array( 5822 );
        learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
        $courses = array(2399,2392,2387,2039);
        foreach($courses as $course){
            ld_update_course_access($user_id, $course, $remove=true);
        }
    }
        
    }else{
    if ( !empty( $user_id ) ) {
        $LD_GROUP_IDS = array( 5410 );
        learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
        $courses = array(2399,2392,2387,2039);
        foreach($courses as $course){
            ld_update_course_access($user_id, $course, $remove=true);
        }
    }}
});
1

There are 1 best solutions below

0
On

Used the $_REQUEST variable to get the meta data using that I used the referrer key to load courses

add_action( 'user_register', function ( $user_id = 0 ){

    if(isset($_REQUEST['referrer']) and $_REQUEST['referrer'] =='https://example.com/youth-register-url/'){
        if ( !empty( $user_id ) ) {
        $LD_GROUP_IDS = array( 5822 );
        learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
        $courses = array(2399,2392,2387,2039);
        foreach($courses as $course){
            ld_update_course_access($user_id, $course, $remove=true);
        }
    }
        
    }else{
    if ( !empty( $user_id ) ) {
        $LD_GROUP_IDS = array( 5410 );
        learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
        $courses = array(2399,2392,2387,2039);
        foreach($courses as $course){
            ld_update_course_access($user_id, $course, $remove=true);
        }
    }}
});