How to Recover a WordPress Username and Password After Migrating to a New Server

A client needed to move a WordPress site to a new host. The most important step was exporting the MySQL database and importing it into the new server. Unfortunately, the only access the client could provide was the FTP account. After some analysis, the situation looked like this:

  • The client had forgotten both the WordPress username and password, so the normal password recovery flow was not an option and the dashboard could not be accessed.
  • The server did not have a proper scheduled backup task. The latest backup was three months old, and a lot of new content had been created since then.
  • The server password was forgotten too. The MySQL user did not allow remote connections, so even though the database credentials could be seen in wp-config.php, the database still could not be exported from a normal MySQL client.
  • The only realistic option was to recover a WordPress admin account, log into the dashboard, install a database export plugin, and export the database that way.

List all users with a WordPress function

Because FTP access was available, we could use built-in WordPress functions to list all users. The key function here is get_users(). In many sites, the user with ID 1 is the administrator. If not, try the other accounts until the admin account is identified. For security reasons, remove the temporary code immediately after use.

Reset the user password with a WordPress function

Once the admin user ID is known, we can reset that account’s password with the wp_reset_password() function. Add the following code to the theme’s functions.php file and then refresh the site once. In the example, 123321 is the new password and 1 is the admin user ID.

wp_reset_password('123321', 1);

After that, log into the dashboard with the new password and install a database export plugin such as WP Sync DB. Once the export is complete, you can import the database into the new server with phpMyAdmin or another database tool.

If the domain name also needs to change during the migration, WP Sync DB can also push the database directly to the new site. The original article does not go into that part in detail, but this method is often enough to get a difficult migration unstuck.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *