• Resolved lucasl9

    (@lucasl9)


    I have a virtual store with woocommerce where I use the USER REGISTRATION plugin to register new customers. This plugin allows the creation of custom fields.
    I created a custom text field called “STATE REGISTRATION”. In this field the user can write something or leave it blank.

    My demand is that when creating a new user, wordpress will read this “STATE REGISTRATION” field and see if there is any content filled in it.

    If you have something filled out, I need the user to have a second “ROLE” added to their profile.

    I created this role before and it’s called “IE”.

    I’m try this, but doesnt works!

    add_action( 'user_registration_after_register_user_action', 'adiciona_role_ie', 9999, 3 );
    function adiciona_role_ie( $valid_form_data, $form_id, $user_id ) {
    	$billing_ie = '';
    	if ( isset( $valid_form_data['billing_ie'] ) && ! empty( $valid_form_data['billing_ie']->value ) ) {
    		echo $billing_ie = $valid_form_data['billing_ie']->value;
    	}
    	if ( ! empty( $billing_ie ) ) {
        
        add_filter('user_registration_after_register_user_action', 'wc_assign_custom_role', 10, 1);
    
        function wc_assign_custom_role($args) {
          $args['role'] = 'ie';
          
          return $args;
        }
    		//update_user_meta( $user_id, 'ie', $role );
    	}
    }

    The form registration: https://i.stack.imgur.com/WHtAm.png

    • This topic was modified 4 years, 2 months ago by lucasl9.
    • This topic was modified 4 years, 2 months ago by lucasl9.
    • This topic was modified 4 years, 2 months ago by lucasl9.
    • This topic was modified 4 years, 2 months ago by lucasl9.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support sanjuacharya77

    (@sanjuacharya77)

    Hi @lucasl9,

    Thank you for the information in the details. Please add the given code to your current theme functions.php file and it will do a trick.

    Here is a code:

    add_action( 'user_registration_after_register_user_action', 'ur_update_role', 10, 3 );
    function ur_update_role( $valid_form_data, $form_id, $user_id ) {
    global $table_prefix;
    $assign_roles_list = array();
    if( isset( $valid_form_data['billing_ie']) && !empty( $valid_form_data['billing_ie']->value ) ) {
    array_push( $assign_roles_list, 'ie' );
    }
    if ( ! empty( $assign_roles_list ) ) {
    // Re-ordering roles according to priority.
    $user_roles_list = ur_get_default_admin_roles();
    foreach ( $user_roles_list as $key => $value ) {
    if ( ! in_array( $key, $assign_roles_list, true ) ) {
    unset( $user_roles_list[ $key ] );
    } else {
    $user_roles_list[ $key ] = true;
    }
    }
    $field_name = $table_prefix . 'capabilities';
    update_user_meta( $user_id, $field_name, $user_roles_list );
    }
    }

    Let me know whether it helps or not and I will get back to you.

    Best Regards!

    Thread Starter lucasl9

    (@lucasl9)

    Hi. Thanks. Perfect!

    This is the best support in the WordPress.

    User Registration plugin team rules!

    Thanks a lot.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Assign user role on new user registration’ is closed to new replies.