How to copy/transfer WordPress sites from one location to another
I'm new to WordPress and thoroughly impressed with the platform... That is until it came time to publish my local MAMP wordpress site to my production server- There's just no clear and straightforward way to transfer a site from one server to another.
Objective:
Transfer a complete wordpress site from one location to another, while maintaining all media, settings, and content.
Existing method limitations:
- Built-in export/import functionality
- Does not tran开发者_StackOverflowsfer themes or settings
- Adds to current content, but does not replace current articles or clear out old ones.
- Doing a sql-dump export and import
- paths are absolute, and are not updated, so links, etc are all broken in new site.
NOTE:
The codex offers this multi-step solution, but I'm looking for something easier. It's not practical for repeated transfers, like in a publishing scenario.
There's no avoiding doing it this way (as far as I know)
- You move the files
- Move the database, using export/import in phpMyAdmin
- Edit your configuration (in /wp-config.php ) to reflect the new DB settings.
- execute the following SQLs in phpMyAdmin:
Update the options for site settings:
UPDATE wp_options SET
option_value = replace(option_value,
'http://oldsite.com/path',
'http://spankingnew.com/otherpath')
WHERE
option_name = 'home'
OR
option_name = 'siteurl';
Update the post contents and the post slugs/urls at the same time:
UPDATE wp_posts SET
post_content = replace(post_content,
'http://oldsite.com/path',
'http://spankingnew.com/otherpath'),
guid = replace(guid,
'http://oldsite.com/path',
'http://spankingnew.com/otherpath');
And you should be done..
Of course there might be some other problems along the way, but I've done it this way a few times.
@Yarin pointed out that there is a much easier way to update the site urls: Just harcode the url settings into /wp-config.php.
In /wp-config.php, add this:
define('WP_HOME','http://{my site path}');
define('WP_SITEURL','http://{my site path}');
This set-and-forget will automatically update all url paths across the database no matter what data you import. It's especially useful if you are constantly importing data from another site, like in a dev-to-production publishing scenario.
A much easier method i have found is to open the MySQL dump in something like notepad++
Then do a find and replace e.g http://www.oldsite.com to http://www.newsite.com
then import the dump using PHP MyAdmin
Amendment
One of the major problems with moving a wordpress site from one domain to the other is mainly down to any plugins or themes that store settings in the database.
The usual search and replace methods don't work because some themes or plug-ins store the data in a serialised format.
say in your theme plugin you have a setting called site home: site home: http://oldsite.com'
Serialised it would be stored in the database like this:
{s:9:"site-home";s:18:"http://oldsite.com";}`
notice the s:**
the serialised array stores the string and how many characters in the string
so if your doing a standard search and replace as described previously
replacing http://oldsite.com to http://mynewsite.com
there are now 20 characters not 18
However a standard search and replace as described above would only update the matched string, the character count does not get updated.
the serialised data would be changed to {s:9:"url";s:18:"http://mynewsite.com";}
This is what breaks wordpress as the data in the serialised array is now corrupt, as the character count is no longer correct.
The best tool in my opinion for migrating wordpress sites is made by some guys at interconnect/it.
It checks serialised arrays and updates the character count as well as standard strings.
If you're migrating a wordpress site, this is the only tool you should be using. and its free
search and replace for wordpress database
Probably the cleanest solution I've found so far: http://wordpress.org/extend/plugins/duplicator/
arnorhs is right on. There are a couple more things that I've run into:
Some hosts (like MediaTemple's DV) won't allow the public user to write files so if that is the case (make sure it's a problem first), you'll want to go /wp-content/uploads/ and make sure that is chmod 777 (or at least 767). All sub-directories need to be the same as well.
Sometimes I've had trouble importing large files into mysql. A shortcut is to open up your exported sql file in a text editor and copy a couple thousand lines to the clipboard and paste them into phpMyAdmin as a SQL call. Works like a charm. Just make sure to end your copy at the end of a SQL statement.
This answer builds off of arnorhs's above, which is correct- I just want to point out a much easier way to update the site urls: Just harcode the url settings into /wp-config.php.
In /wp-config.php, add this:
define('WP_HOME','http://{my site path}');
define('WP_SITEURL','http://{my site path}');
This set-and-forget will automatically update all url paths across the database no matter what data you import. It's especially useful if you are constantly importing data from another site, like in a dev-to-production publishing scenario.
(NOTE: If you ever get a server error with this, you may have to switch permalinks on/off in the admin panel to get url path changes to register)
For more on this, see: Wordpress codex: Changing the Site URL
Some other notes...
If you are trying to move/replace a sites contents to another, existing site, you would:
- Only need to copy over the contents of 'themes', 'plugins', 'uploads', and 'includes/languages' folders. (Correct me if I'm wrong)
- Would want to clear out existing MyQSL data before doing your import: Select all tables and choose 'empty' in PhpMyAdmin.
Yep, one of the more hideous undocumented bugs of WordPress :(
I wrote a plugin that solves the entire problem, correctly - http://wordpress.org/extend/plugins/root-relative-urls/
Basically it will transform all links in your WordPress install to be root-relative (ie start with a "/" which are effectively equivolent to absolute urls.) And it will strip the domain from links to images and other assets as they are added to the TinyMCE editor so when you create new content it will intelligently alter those links too.
With this plugin you can then access your development site via IP address, Local Host, or any alias you want to give it. And when you do a database dump of the data or if you just use the import/export utility, your content will simply work as you'd expect in production.
It doens't work if your development environment has a different root level structure from your production environment but you can use .htaccess rewrite rules to handle that edge case - still without having to do dangerous search and replace string operations on your exported sql data.
It also doesn't work with subdomain multisite installs, but that's an even larger can of worms.
精彩评论