• Resolved damiansmith86

    (@damiansmith86)


    Hey,

    I am trying to set the user role based on which “company” a user selects.
    Company A = Role A
    Company B-Z = Role B

    I can’t get this to work with conditional logic though, I thought setting the form default to Role B and then having the role dropdown with only Role A defined to show when Company A is selected would work, but as the field is only hidden, it is always setting the role as Role A.

    Is there anyway of doing this? I don’t want users to select their own role as could select wrong one.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter damiansmith86

    (@damiansmith86)

    Ive found this snippet that sets the role based on email, is there a way to tweak it so change based on “company” selected?

    add_action(“um_registration_complete”,”um_042121_change_user_role”, 1, 2 );
    function um_042121_change_user_role( $user_id, $args ){

    if( isset( $args['user_email'] ) ){

    list($user, $domain) = explode('@', $args['user_email'] );

    if ($domain == 'email.co.uk') {
    wp_update_user( array( 'ID' => $user_id, 'role' => 'um_staff' ) );
    }

    }

    thanks

    • This reply was modified 1 year, 11 months ago by damiansmith86.
    Thread Starter damiansmith86

    (@damiansmith86)

    Tweaked the code myself and seems to be working as expected… just incase anyone else needs this:

    add_action("um_registration_complete","um_042121_change_user_role", 1, 2 );
    function um_042121_change_user_role( $user_id, $args ){
    if ( isset( $args['company'] ) && strstr( $args['company'], 'Company Name' ) ) {
    wp_update_user( array( 'ID' => $user_id, 'role' => 'um_staff' ) );
    }
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘User role based on conditional logic’ is closed to new replies.