Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davidmutero/bddd9e184a1b8e5ced45173672259e3b to your computer and use it in GitHub Desktop.

Select an option

Save davidmutero/bddd9e184a1b8e5ced45173672259e3b to your computer and use it in GitHub Desktop.
Update the user_nicename field in the database after changing a username.
<?php
/**
* Update the user_nicename field in the database after changing a username.
*
* Use this snippet after updating a user's username (user_login) using a plugin like
* "Easy Username Updater" or similar tools. This ensures that the profile URL updates
* correctly by setting the user_nicename field to match the updated username.
*
* Instructions:
* 1. Add this snippet to your theme's functions.php file or use a code snippets plugin.
* 2. Replace 'USER_ID_HERE' with the actual user ID of the member whose URL you want to update.
* 3. Save the changes and visit the site to execute the code.
* 4. IMPORTANT: Remember to remove or disable the snippet after execution to prevent unintended updates.
*
* Read more about customizations:
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
add_action('init', function() {
$user_id = USER_ID_HERE; // Replace with the user's ID.
$user = get_userdata($user_id);
if ($user) {
// Sanitize the username to create a URL-friendly user_nicename.
$sanitized_nicename = sanitize_title($user->user_login);
// Update the user_nicename in the database.
wp_update_user([
'ID' => $user_id,
'user_nicename' => $sanitized_nicename,
]);
// Flush rewrite rules to update the URL structure.
flush_rewrite_rules();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment