admin管理员组

文章数量:1422007

I allow users to sign up with their first name, last name and e-mail addresses. I want to make their first and last name as slug. For example user first name is “John” and last name is “James”. The slug should be example/users/john-james/ I do not want to use any plugin to make changes.

There are already 300 registered members on the website and i want to change their slug too.

I allow users to sign up with their first name, last name and e-mail addresses. I want to make their first and last name as slug. For example user first name is “John” and last name is “James”. The slug should be example/users/john-james/ I do not want to use any plugin to make changes.

There are already 300 registered members on the website and i want to change their slug too.

Share Improve this question asked Jul 3, 2019 at 8:36 JackJack 133 bronze badges 3
  • Hey, you have to change their user_login meta to achieve that. PS : By default the user_login is composed by the first and last name of a user ! – Rachid Chihabi Commented Jul 3, 2019 at 8:54
  • @RachidChihabi Could you please let me know how can i do this? – Jack Commented Jul 3, 2019 at 9:06
  • i just put an answer cuz comments are limited...see bellow – Rachid Chihabi Commented Jul 3, 2019 at 9:31
Add a comment  | 

1 Answer 1

Reset to default 0

To update existing users, try to make a script file and put that code on it (you have to require necessary file wp-load.php if you put that file in the root of your wp instance) or by listning on a hook like init hook :

$blogusers = get_users( 'role=subscriber' ); //get users by role
// Array of WP_User objects.
foreach ( $blogusers as $user ) { //loop throught users
    update_user_meta($user->ID, 'user_login', sanitize_title($user->first_name.' '.$user->last_name)); //update user login

    //if $user->first_name / $user->last_name didn't work, try to get the first and last names by using get_user_meta($user->ID, 'first_name', true)...
}

if you wanna use that behaviour just after user has registred, you have to use the user_register hook, and then put the same code above in your function...

PS : Code not tested.

本文标签: profilesMake user’s first and last name as user slug