Reply To: Action hook "user_registration_after_register_user_action"

Home Forums User Registration Action hook "user_registration_after_register_user_action" Reply To: Action hook "user_registration_after_register_user_action"

#3126

rivan
Participant

Okay I finally find out how to do a such thing, here is the solution for changing the users role for example.
The $user_id parameter which is passed in the hook is made of following things:


[07-Aug-2018 16:28:36 UTC] This is the content of user_id element: array(4) {
  ["user_login"]=>
  object(stdClass)#12761 (5) {
    ["value"]=>
    string(5) "test3"
    ["field_type"]=>
    string(4) "text"
    ["label"]=>
    string(8) "Username"
    ["field_name"]=>
    string(10) "user_login"
    ["extra_params"]=>
    array(2) {
      ["field_key"]=>
      string(10) "user_login"
      ["label"]=>
      string(8) "Username"
    }
  }
  ["user_email"]=>
  object(stdClass)#12762 (5) {
    ["value"]=>
    string(14) "test3@test.com"
    ["field_type"]=>
    string(5) "email"
    ["label"]=>
    string(10) "User Email"
    ["field_name"]=>
    string(10) "user_email"
    ["extra_params"]=>
    array(2) {
      ["field_key"]=>
      string(10) "user_email"
      ["label"]=>
      string(10) "User Email"
    }
  }
  ["user_pass"]=>
  object(stdClass)#12763 (5) {
    ["value"]=>
    string(4) "test"
    ["field_type"]=>
    string(8) "password"
    ["label"]=>
    string(13) "User Password"
    ["field_name"]=>
    string(9) "user_pass"
    ["extra_params"]=>
    array(2) {
      ["field_key"]=>
      string(9) "user_pass"
      ["label"]=>
      string(13) "User Password"
    }
  }
  ["role_checkbox"]=>
  object(stdClass)#12765 (5) {
    ["value"]=>
    string(45) "cond1,cond2,cond3,cond4"
    ["field_type"]=>
    string(8) "checkbox"
    ["label"]=>
    string(12) "Role chooser"
    ["field_name"]=>
    string(13) "role_checkbox"
    ["extra_params"]=>
    array(2) {
      ["field_key"]=>
      string(8) "checkbox"
      ["label"]=>
      string(12) "Role chooser"
    }
  }
}

Where role_checkbox is the fields name that I gave to the custom Checkbox in the UserRegistration form creator !
So to use it one can do it like that:


add_action( "user_registration_after_register_user_action", "user_registration_role_checkbox" );
function user_registration_role_checkbox( $user_id ) {

    $role = $user_id["role_checkbox"]->value;

    if( strpos($role, "cond1")!==FALSE && strpos($role, "cond2")!==FALSE ){
        $new_role = 'role3';
        $user = new WP_User( $user_id["user_login"]->value );
        $user->set_role($new_role);
    }
    elseif ( strpos($role, "cond1")!==FALSE ) {
        $new_role = 'role1';
        $user = new WP_User( $user_id["user_login"]->value  );
        $user->set_role($new_role);
    }
    elseif ( strpos($role, "cond2")!==FALSE ) {
        $new_role = 'role2';
        $user = new WP_User( $user_id["user_login"]->value );
        $user->set_role($new_role);
    }
}

Just to explain a little bit: here cond1, cond2, … are the choices that has the user in the checkbox. When he validates the form all the conditions that were checked will be listed (separated by a comma) in the role_checkbox field of the $user_id array. Then in the code I have chosen for example that if cond1 and cond2 were checked => role3 is assigned to the user, if only cond1 => role1 and if only cond2 => role2.

That’s it !

Hope that it can help some other programmers 😉

Rivan