Change a WP user password

Had to do this for the “admin” user on a WordPress blog that was being imported from the outside. For a marketable skill, read on.

The WP Codex almost has this right, but messes up on a key bit of syntax.

1. You need an MD5 hash of the new password’s text:

echo -n "thisismynewpass" | md5sum | cut -f1 -d' ' >pw.txt

Pay close attention to spacing here.

The file pw.txt will contain the hash. Using the example above the string you get should be:

4a8197831a1f58bef78afbeb2f99049b

2. Use mysql to connect to the database as its owner or the root user.

3. Switch to the target database:

use targetdb;

4. List its tables:

show tables;

Output will look something like:

+-----------------------+
| Tables_in_idctemplate |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
11 rows in set (0.00 sec)

It is common to see a table prefix (the “wp_”), especially where there are multiple sites being supported by a single MySQL server. Ideally the prefix reflects the database name (but rarely does).

From the listing above you’d determine that the users table is named “wp_users”.

5. Query for the users in the users table:

select ID,user_login,user_pass FROM wp_users;

Output will look something like this:

mysql> select ID,user_login,user_pass FROM wp_users;
+----+------------+------------------------------------+
| ID | user_login | user_pass                          |
+----+------------+------------------------------------+
|  1 | admin      | 1010101010101010101010101010101101 |
+----+------------+------------------------------------+
1 row in set (0.00 sec)

6. Now update the password for “admin” using the following query:

update wp_users
set user_pass = "4a8197831a1f58bef78afbeb2f99049b"
where ID = "1";

7. Confirm things are as you want by running the query from step 5 again.

Note that there are other fields you might want to change for the admin user, like “user_email”, but those can also be changed via the WordPress admin dashboard if you’re impatient or deathly afraid of the command line.