Home > Net >  Wordpress update user meta doesn't work with strtolower
Wordpress update user meta doesn't work with strtolower

Time:01-03

I've a problem with the wp_update_user in wordpress. The code below works, but not as I want.

add_action('user_register', 'register_role', 10 , 1);

function register_role($user_id) {
    $userdata = array();
    $userdata['ID'] = $user_id;
    $userdata['role'] = $_POST['Newrole']; //value for example 'Goldmember'
    wp_update_user($userdata);
}

I have several problems:

  • the "Goldmember" is the displayname of the userrole. I see the new usermeta in the database (a:2:{s:6:"Goldmember";b:1;s:6:"subscriber";b:1;})
  • the "Goldmember" doesn't work in wordpress user administration, and the user has the default role activated
  • if I change "Goldmember" to "goldmember" with strtolower($_POST['Newrole']) the code doesn't work anymore
  • if I change the "Goldmember" to "goldmember" in the database (a:2:{s:6:"goldmember";b:1;s:6:"subscriber";b:1;}), it is visible in wordpress user administration and works

what do I have to change in my code, I don't see any mistake.

thanks a lot

CodePudding user response:

I've changed my way to solve the problem. I took an other hook to change the userrole on registration. I took the "uwp_after_process_register", this one will be fired at the whole end of the registration process, so there I can do, whatever I want.

This is now my solution, so every problem I had is solved, you only have to take UsersWP:

add_action('uwp_after_process_register', 'register_role', 10 , 2);

function register_role($data, $user_id) {
    $userdata = array();
    $userdata['ID'] = $user_id;
    $userdata['role'] = $data['FormFieldRole'];
    wp_update_user($userdata);  
}
  • Related