Change Roles after Registration

enter image description here

I have a registration process that registers a user:

  • Assigns a custom role (wholesale) if they registered using that specific form and then a hold is assigned “pending”
  • I then get an email, review and approve the registration
  • The instance works on my local instance but when I deploy to a live site with W3 Total Cache and CloudFlare, somehow the whole logic doesn’t work. The issue with the above is the “wholesale” role is not stored as indicated below:

    So my next approach is to have another logic (below), that runs through the list of users and checks if a role exists. If there is none, it changes the status to “Pending” and assigns “Wholesale” to the user’s role.

    // get user meta
    $user_meta = get_userdata($user_id);
    
    
    // get user roles
    $user_roles = $user_meta->roles;
    
    
    //if ( !is_array($user_roles) && !empty( $user_id ) ) {
    if ( $user_id ) {
    
        // check id, find and replace role
        $u = new WP_User( $user_id );
        $u->remove_role('wholesale');
        $u->set_role( 'wholesale' );
    
    
        // deal with the default registration
        $status = UR_Admin_User_Manager::PENDING;
    
        $user_manager = new UR_Admin_User_Manager( $user_id );
    
        //The user have to be not alerted on status creation, it will be always pending or approved
        $alert_user = false;
    
        $user_manager->save_status( $status, $alert_user );
    
    
    } else if ( in_array( 'wholesale', (array) $user_meta->roles ) ) {
    
    
        $status = UR_Admin_User_Manager::PENDING;
    
        $user_manager = new UR_Admin_User_Manager( $user_id );
    
        //The user have to be not alerted on status creation, it will be always pending or approved
        $alert_user = false;
    
        $user_manager->save_status( $status, $alert_user );
    
    
    } else if ( $this->is_admin_creation_process() ) {
    
    
        // If the user is created by admin in the backend, than automatically approve him
        $status = UR_Admin_User_Manager::APPROVED;
    
        $user_manager = new UR_Admin_User_Manager( $user_id );
    
        //The user have to be not alerted on status creation, it will be always pending or approved
        $alert_user = false;
    
        $user_manager->save_status( $status, $alert_user );
    
    
    }
    

    Read more here:: Change Roles after Registration

    Leave a Reply

    Your email address will not be published. Required fields are marked *