Manually Moving a WordPress Site to Localhost/WAMP

When I’m asked to make changes to a live WordPress site (especially code changes for plugins and themes) I like to take a local copy for development. Moving a WordPress site to a local host is not too difficult, but there are a few problems which you will probably find along the way.

WordPress

When I’m asked to make changes to a live WordPress site (especially code changes for plugins and themes) I like to take a local copy for development. As I have a Windows PC, I use WAMP to give me a LAMP development stack.

Manually moving a WordPress site to a local host is not too difficult, but there are a few problems which you will probably find along the way. One of these is that WordPress likes absolute URLs. I think it makes life easier for themes and plugins, but when you move to a local host it’s often a problem.

Copy the Files

The first thing to do is to copy your live website to a folder in your wamp/www folder. You don’t need to call it the same name as the website. You can copy with FTP.


Before you start the transfer you may want to check the size of the wp-content\uploads folder. This is where the media files get uploaded to and it can get very large – as in gigabytes. You may not need all of these immediately and your browser will soon flag any missing images.

Make a Database

I find the easiest way is to use phpMyAdmin and export/import the MySQL database. WAMP has phpMyAdmin built-in and you can find it by clicking on the WAMP icon in your taskbar. Your web host will probably provide you with the same tool on the live server.

On your live server run up phpMyAdmin and then you can export the database as SQL and save/download as a ZIP file. On your local phpMyAdmin create a new database (it can be a new name if you want) and then import the file.

Unless the zip file is extremely large this should be straightforward. If you get size errors there may be a message on how they can be fixed. Otherwise you may have to export large tables in chunks.

Running the site

As my Windows PC is running IIS and thus using port 80, the Apache server running from WAMP is set to port 85, so the URL of my local web site will be http://localhost:85/foldername.

You can try this now if you like, but you’ll almost certainly see “Error establishing a database connection“, so let’s fix that next.

Database Connection

Your database connectivity settings will need changing to whatever your local MySQL database settings are (you can set these in phpMyAdmin).

Open the file wp-config.php which will be in your root folder and change the database connection settings. The host will always be localhost but you will need to provide the database name, username and password.

// ** MySQL settings ** //
/** The name of the database for WordPress */
define('DB_NAME', 'mydbname');

/** MySQL database username */
define('DB_USER', 'myusername');

/** MySQL database password */
define('DB_PASSWORD', 'mypassword');

/** MySQL hostname */
define('DB_HOST', 'localhost');

 

The Site is still not working!

Although we’ve done the bulk of the copy, if you try to run the website now you’ll see it’s still broken. This is what happened on one of my sites:

WpPrePostMoveWordPress is obviously running and getting data from the database, but I’ve lost images and formatting. This is all to do with WordPress Settings and due to WordPress using absolute URLs so frequently.

WordPress and PHP try to be as forgiving as possible but in some ways it would be better to fall over and tell me whats wrong; it doesn’t and every link points to the home page.

What’s worse is that when I try to log into the admin site (this should be at http://localhost:85/foldername/wp-admin) the page just goes back to home. I can’t get into admin to change the settings!

Fix the links in the database

We can use phpMyAdmin to run a query to fix the links in the database. I used the WordPress recommendations:phpMyAdmin

If that fixes your problems then you’re away. In my case I still couldn’t get into admin so I made the following modifications in the wp-config.php just above the database setting:

define('WP_HOME','http://localhost:85/myfoldername');
define('WP_SITEURL','http://localhost:85/myfoldername');

That forces WordPress to use these settings instead of the database settings. All was well after that.

There may be a few tools or plugins that may make the job a little easier. The manual method is not too difficult and at least you can control what happens.