6

I'm just wondering about usernames... Why isn't is possible to change this through Wordpress API? (I understand why a user in the admin dashboard can't change username(s) but that isn't really same thing!?)

Code below does just ignore the user_login - setting.

$pupil_obj= get_post($postid_pupil);

$user_login = $pupil_obj->post_name;
$user_nicename = $pupil_obj->post_name;

$user_args = array(
    'ID'            =>  $current_user->ID,
    'user_email'    =>  $email,
    'user_login'    =>  $user_login,
    'user_nicename' =>  $user_nicename                    
);                
wp_update_user( $user_args ) ;

I could update the username to the database directly through $wpdb. This makes no sense to me. Can someone explain?

4
  • 2
    I know HOW to do solve the issue, but I was asking WHY I have to directly against the db instead of through the API. I think it would make more sense for it to be available through the API, because the API could control diffrent things/errors/warnings that could error when changing the username. Commented Feb 20, 2014 at 12:53
  • Directly editing the value in database or using a plugin is the best method, I've never had an issue modifying the user name (user ID on the other hand should never be modified)
    – nodws
    Commented May 24, 2018 at 17:42
  • It's not advisable to update your username using code like @bestprogrammerintheworld's simple update. Try using the wp-cli, to safely replace all occurrences, verifying the changes first: wp search-replace --regex '\bOLD_USERNAME\b' 'NEW_USERNAME' --log=- --dry-run
    – Walf
    Commented Dec 19, 2019 at 23:58
  • @Walf I read wp-cli is recommended to change the domain name of a WordPress website (because of the length of the URL that is used sometimes: wordpress.org/support/article/moving-wordpress/…) but to change the username, it is possible to do it properly without wp-cli: wordpress.stackexchange.com/questions/277650/… (I moved my answer to another question, which I did not find when doing a search with an external search engine).
    – baptx
    Commented Apr 8, 2021 at 8:53

2 Answers 2

6

Looking on Trac I found a ticket that discusses exactly this issue: Administrator should be able to change usernames

This is what it all boils down to:

Changing usernames could break permalinks

To prevent this a lot more than just changing the username in the DB would need to be done. E.g. redirection from old usernames.

Caching Issues

I quote from the aforementioned Ticket:

"One problem you could get, if you do this using the db while using memcached, is the the old value remains in the store until memory runs out."

Denis-de-Bernardy

TL;DR

It may cause various problems and therefore is intentionally difficult to do. In case you think of this differently fell free to join the discussion in the corresponding ticket.

3
  • 3
    So basically - you can't do it through the API because that would make the WP - developers responsible? ;-) Commented Feb 20, 2014 at 12:54
  • Kind of. It seems as if they only want to have this feature if it works in every situation with no bad side-effects. But if you want to discuss this, WPSE probably isn't the right place, that is why I added the link to the Trac ticket.
    – kraftner
    Commented Feb 20, 2014 at 13:05
  • 2
    Ok, thank you! :-) (I asked because I thought there was some real issue with the change of usernames. It seems it's all about taking precaution) Commented Feb 20, 2014 at 13:33
2

There is a note on the wp_udate_user function codex page: http://codex.wordpress.org/Function_Reference/wp_update_user. And that is:

Please note that we cannot change the usernames through this function, in fact the usernames cannot be changed from the admin dashboard as well since WordPress does not allow the usernames to be updated.

So you can't change the username using wordpress built in functions because Wordpress developers think it's not a good practice. There really are not any other explinations to this besides the one listed in the Note.

2
  • 1
    This is not exactly an answer, because I've already mentioned this in my question. My real issue was WHY it isn't recommended and WHY can't I do it through the Wordpress API? Commented Feb 20, 2014 at 12:37
  • I edited my comment. The first part was the actual answer. The MySql part was just about the only solution there is. Commented Feb 20, 2014 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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