Migrate WordPress Site from Local Development to Live Server and fix serialized data issues

Published on Author Pradip Dey5 Comments

Migrating WordPress Manually

The manual way to migrate a WordPress site to a live server is in a few steps:

  • Copy the all the contents of the WordPress installation in your development webroot to your webroot in your live server via FTP, cPanel or other transfer method.
  • Export/Dump your development MySQL database
  • Create a new MySQL database for your live environment
  • Import the development database into the blank live environment database
  • Change the URLs in the database from old to new
  • Update /wp-config.php file for new database, database username and password

If you have created your test site with a different URL then the finishing live site which is more than likely, (unless you are using Virtual Hosts and are swapping between local and remote IP addresses) you may run into issues with some data not being migrated to the live site.

This data may include WordPress widgets not showing, certain plugins or theme data is missing. The reason this data may not have made the migration trip is because it is lost as the data has been serialized in the database with the old URL and then can’t be unserialized as there is a new URL.

Serialized Data

So what is Serialized data?  Well it is data that has been written as an array in PHP and stored as a string in one field in a database as appose to numerous fields in a mysql database. This allows a developer an easy, quick and efficient set up but comes at the price of serializing and unserializing that data.

WordPress uses its own serialization functions, however the disadvantage of this approach is that the data is now not as easily transported to say a new URL as the URL is used as part of the serialization.

Temporarily Fixing Serialized Data Issues

To get you out of an immediate bind and show any missing data after a migration you can edit a function in a core WordPress file:

/public_html/wp-includes/functions.php

Comment out the function maybe_unserialize() on lines 230-234 and replace with:

1
2
3
4
5
6
7
8
9
10
11
12
13
function maybe_unserialize( $original ) {
    if ( is_serialized( $original ) ) {
        $fixed = preg_replace_callback(
            ‘!(?<=^|;)s:(\d+)(?=:”(.*?)”;(?:}|a:|s:|b:|i:|o:|N;))!s’,
            ‘serialize_fix_callback’,
            $original );
        return @unserialize( $fixed );
    }
    return $original;
}
function serialize_fix_callback($match) { return ‘s:’ . strlen($match[2]); }

Obviously you will lose that when WordPress is upgraded and is not considered best practice so a permanent solution is required.

Permanent Fix for Serialized Data Issues

The better way to go out this is exporting a database which already references the new URL of the live environment and this is where WP-Migrate DB sorts out the problem. Install the plugin in your development site and export the database with the new URL and file path stated.

migrate-wordpress-database

Once this is done just import the exported database into the new live server.

 

WordPress Migration Plugins

migrate wordpress siteA couple of WordPress plugins that can deal with migrating a site from development to live server and deal with any data serialization issues are Duplicator and BackUp Buddy the former is free and the latter is a commercial plugin. One of the key benefits of BackUp Buddy is that it can migrate WordPress Multi-Site installations.

I use mostly Duplicator which I have found to be faultless in many site migrations and the plugin has been around for a while now.

You add the plugin to the the site you are migrating from and create a package from the plugin which includes all the WordPress content and the database, you upload that package to your new server and you need to have a blank MySQL database ready.

From there you need the credentials of the new database; name, username and password – after the package is uploaded go to the config file via a browser, fill in the blanks and you have a migrated site.

Here are the full Duplicator docs.

5 Responses to Migrate WordPress Site from Local Development to Live Server and fix serialized data issues

  1. I think your website was much helpful for me. when u keep going this perfect work we’ll visit again at your weblog!

Leave a Reply

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