Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

WordPress Live/Prod migrating back to Local/MAMP

I setup WordPress locally using MAMP and migrated to a live/PROD environment. When setting up locally it does not include

define('WP_HOME', 'http://yourdomain.com/'); define('WP_SITEURL', 'http://yourdomain.com/');

in the wp_config.php file. That I added when migrating to the live/PROD environment. Now I migrated back to local to do some edits and it was not working with removing

define('WP_HOME', 'http://yourdomain.com/'); define('WP_SITEURL', 'http://yourdomain.com/');

I changed it to the same path my browser sees it when MAMP is running

define('WP_HOME', 'http://localhost/localwp.com/'); define('WP_SITEURL', 'http://localhost/localwp.com/');

and that kind of worked but when changing things I would get a blank browser window and would have to enter

localhost/localwp.com/wp-admin

to get the dashboard back up. Now I am having issues and things are wonky, localhost/localwp.com/wp-admin is a blank page, can't login.

What is the standard practice when migrating from a live server to local concerning the

define('WP_HOME', 'http://yourdomain.com/'); define('WP_SITEURL', 'http://yourdomain.com/');

Why does WordPress leave this off when installing locally in the first place?

2 Answers

Hi,

Migrating between domain names is a little more complex than that.

There are a number of places in the database where the might be stored ( in post content, some options) and they all need to be updated. A normal database search and replace can cause problems as some times the url's are stored in serialized fields and the search and replaces makes these invalid.

However, there are some tools out there designed to do this. I'll explain how i do it below.

You'll need to be a bit familiar with the command line to do this, but its not hard stuff and works on Mac and Windows

The tool you will need to install is WP-CLI. If your not on a *nix machine you should have a look at Alternative Install Methods.

Tree house have an article about this tool too. Tame WordPress from the Command Line with wp-cli

Once you have checked WP-CLI is installed properly you should be able to run wp at the command line and see the list of available commands.

The command you are looking for is search-replace

Open up a terminal and navigate to your Wordpress install. If the project is at /home/username/my-project you would type cd /home/username/my-project

Now you can start to use the wp tool. The first thing to do is make a back up of your database.

wp db export backup.sql

This will create a backup you can use to revert if something goes wrong.

Assuming we are moving a site from http://localhost/wordpress to http://example.com you would use the following command.

wp search-replace http://localhost/wordpress http://example.com

This will show you how many times the url was changed and in what tables.

At this point you can export your database again and use the SQL file on your server.

wp db export live.sql

These SQL files will reside in your Wordpress installation folder, once you have done your migration you should remove these files. Make sure that you don't accidentally upload them to your server as it poses a security risk.

Thanks for the answer, I am familiar with the need to export/import and especially update the database URLs, I have done this. What I am asking is why when WP sets up on a local environment, such as when running MAMP, does it it not include the define('WP_HOME', 'http://yourdomain.com/'); define('WP_SITEURL', 'http://yourdomain.com/'); into the wp_config.php file? I have migrated a local environment install to a live server and added this, since it is required. This was my first time taking a WordPress site running on a live server and trying to migrate to a local environment. When doing this I first deleted the WP_HOME and WP_SITEURL, since a fresh install running locally will not include them and WP runs fine. That did not work so I added them back and replaced the live server URLs with the local environment URLs, i.e. define('WP_HOME', 'http://localhost/localwp.com/'); define('WP_SITEURL', 'http://localhost/localwp.com/'); This worked but there seem to be some issues at times. Now I can not get the admin login URL to open. For a while the main issue was when applying a change in the dashboard, it would refresh to a blank page. It was annoying but I could retype the admin URL into the browser and the dashboard page would be back.

So my Qs are: Why does WP leave out the WP_HOME and WP_SITEURL in the wp_config.php when doing a local install initially, and still run fine? What is the protocol for defining, updating or removing WP_HOME and WP_SITEURL, in wp_config.php, when migrating WordPress from a live server to a local environment?

In all the Wordpress sites i've migrated i've never used those 2 constants.

They aren't strictly needed and it seems aren't the best practice but they can help you when moving sites. Changing The Site Url

It seems like those constants can be used to override the options in the database. By default wordpress sets up those values in the database.

In your situation i would use my method above to update the url's and remove those constants from your wp-config.php

Thanks, I will try that. It was here on Treehouse I was taught the migrating technique of editing the wp-config.php and adding the:

define('WP_HOME', 'http://yourdomain.com'); define('WP_SITEURL', 'http://yourdomain.com');

Migrating WordPress from Local to Live Server

It does seem the codex link below states otherwise. Wonder if this has to do with the initial setup/install being done locally with MAMP? This was my first attempt at a local environment install. After local install I migrated to my DEV server, made some changes, then migrated back to local. Wanted to see how this worked for possible future development of sites.

The Codex link you supplied looks like it has the answers I will need: CODEX - Changing The Site URL

Thanks for the help.