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.com/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.

  • 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 Jul 3 at 8:54
  • @RachidChihabi Could you please let me know how can i do this? – Jack Jul 3 at 9:06
  • i just put an answer cuz comments are limited...see bellow – Rachid Chihabi Jul 3 at 9:31

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.

  • thanks! I will try this code and get back to you. – Jack Jul 3 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.