Moving a WordPress Site

Moving a WordPress site from one domain to another is fairly easy. You copy the files and then the database from the old site to the new site. Once done, you have to go through the database and replace all references to the old domain to the new domain. So, for example, “www.oldwebsite.com” becomes “www.newwebsite.com”. You can do this either by making the changes in the MySQL dump file before uploading to the new database. Or, you can use a find and replace within MySQL across the database.

In doing this, however, you discover your WordPress site isn’t working or doesn’t look the same. You may wonder what happened and have to spend hours fixing the problems.

The issue lies in serialized arrays within the database. Serialized arrays have a size measurement of the following string. When a serialized array is converted into an array, it will fail if those sizes don’t match the actual length of the string. As such, unless the two domain names are exactly the same size, WordPress won’t be able to parse the serialized array and the option will not be applied.When it comes to theme settings, this becomes immediately apparent.

One solution for correcting this problem is to write a PHP script to fix serialized arrays using regular expressions. This too has problems.  The regular expression attempts to look for values between two quotation marks. If the data within the value also contains quotations marks, the regular expression will stop at the wrong quotation mark and the size will still be interpreted incorrectly. We could write a more complex regular expression to ensure the serialized array is fixed, but there is a better solution.

Instead, we can write a script that will go throughout the entire WordPress database and replace the references when found. Part of this process would include unserializing an array and then updating the strings within it. The script below does just that.

Leave a Reply

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