How to change a user’s password programatically

The question:

I am trying to create a totally different profile page (not the one provided by WordPress), since the layout and feel of the default profile page is too “WordPress” for the users. Now I am successful in being able to present a page that can modify user meta data like firstname, lastname, city, zip code, etc.

Now what stumped me is changing the password of the user. Is there any built-in wordpress function, say change_user_password($user_id, $new_password)? I’m surprised that I can’t look for anything that does this.

Worse comes to worst I’m doing an UPDATE wp_users SET user_pass = md5($new_password) WHERE ID = $user_id, but I would be really amazed if there is no function for this one.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

wp_set_password( $password, $user_id );

See reference for details.

Method 2

A simple wp_update_user(array('ID' => $userid, 'user_pass' => 'myNeWpaSSword')) will do everything for you – wp_update_user() (Codex).

On a sidenote: are you aware of the Theme My Login (WordPress Plugin)? It can provide you with a front-end side profile page.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment