Migrating from WordPress.com   Leave a comment

For a variety of technical reasons (more control over the domain, ability to run entirely custom PHP, potential future tech projects), I recently moved this blog from WordPress.com to its current home, a “self-hosted” WordPress installation on a Bluehost server. While the process was fairly straightforward and instructions are available, there are some “gotchas” with workarounds that bear explaining. This article gets fairly technical, so read on if you’re so inclined. I’d like to emphasize that I’m not a WordPress guru by any stretch, so I’m just describing what worked for me.

Splitting WordPress Items

As explained in the standard tutorials, the easiest way to transfer your posts, pages, etc. to a new WordPress installation is to use the inbuilt exporter. The problem I ran across is that the exported file was a bit too large. When I imported it into my new blog (selecting “Download & import file attachments”) the import process would timeout. At first, I attempted to “solve” this by repeatedly running the import process, as it seemed to resume where it had been cut off. However, I soon realized that there were odd errors involving the attachments (ie images) that were processing at the time each intermediate import aborted.

My solution was to split the exported .xml file and then upload it piecemeal. There are other sites that describe this process, but they mostly focus on the 2MB upload limit, not the php execution time limit that was frustrating my attachment download attempts. For that reason, I wrote my own quick Python script to break the file into manageable pieces (for me, 90 items per file).

The exported .xml file has three major sections. At the top is what I’ve come to think of as the preamble, the bit before the first <item> tag. It contains blog title information, categories, etc. At the very end of the file is the footer, a few closing tags. Between are the <item> blocks that describe each post, page, image, etc. in your blog. The key to splitting up the .xml file is that each sub-file contain the header, some items, and the footer. My script, below, requires Python 2.5 or later, and takes one argument, the path to the .xml file to be split. You can modify line 12 to change the number of items per output file.

Post Parents

In WordPress, posts (ie all objects) can have “parents”. This is particularly useful for image attachments, which are often “children” to an individual post. If that post contains a [gallery] shortcode, WordPress will render a lovely clickable array of thumbnail images. For whatever reason, although post parent data is in the .xml export file, WordPress didn’t seem to import it. The following Python script parses the .xml file and generates a file containing a list of SQL commands that, when run in the context of your WordPress site’s database (for instance, using phpMyAdmin which is built into the Bluehost cPanel), will reestablish the correct parental relationships between your posts, and hopefully fix any broken galleries. The first argument is the path to the .xml file; the second is the path to the output SQL file.

Image Sizes

Of course, you’re going to need to use some kind of tool (I recommend John Godley’s “Search Regex” plugin) to fix links and <img> tags throughout your posts. There are plenty of pages that describe how regex’s work, and my goal isn’t to duplicate that here. But one “gotcha” moving away from WordPress.com has to do with image size. WordPress.com has a nice image server which interprets URLs like: http://blogname.files.wordpress.com/2014/01/file.name?w=300. It seems the files.wordpress.com server efficiently serves up the file, resized to the width specified, saving your users from downloading a huge version of an image that will appear smaller on their screen. While WordPress will by default make a few different standard-size images from each upload, those sizes are not easily changed on a per-image basis, don’t allow modifying the size from within the post, and don’t have immediately obvious filenames.

To get around this, I created a quick PHP script which serves up a resized version of any image given the desired width. The script should be straightforward, but a few things bear note. First, I have this in a subdirectory of my webserver, so make sure to modify line 37 with the correct relative path to wp-load.php. Because it uses WordPress’s internal image resizing technique, it doesn’t need any additional libraries. Second, line 27 ensures that the resized images are cached by clients and proxies. This is to prevent the default behavior of interpreting the width parameter as a form submit and therefore not caching. Finally, notice that this script only resizes an image the first time it is requested at a given width — after that, the resized image is saved for future use. If you play around requesting lots of different sizes, it might be worth deleting the extras when you’re done.

In order for this script to function, you will want to modify your .htaccess file to redirect image requests including the w get parameter to it. Replace mcwp/mcimage.php on line 4 with the path to your copy of the PHP above.

If, after implementing this, you don’t want all of the resized images that WordPress automatically generates bloating your server, you can remove them.

Redirect Detection

Once your new blog is set up, you’ll probably want to redirect visitors from your old blog. WordPress.com offers a Site Redirect feature which redirects requests for your old blog (including the target URI) to your new blog (or a URL of your choice). The redirection is accomplished using a (search engine friendly) HTTP 301 “Moved Permanently” response, which in theory means that intelligent search engines will update their records and viewers will be seamlessly redirected.

However, there’s not an easy way for your new site to determine whether a given viewer was redirected — and thereby tell them to update their bookmarks, etc. One option is to post an “update your bookmarks” to everyone for a while, but that might grate on the eyes. My solution was to create an intermediate redirection page, a simple PHP script which creates a cookie and then — using a second 301 redirect — passes the user to the actual blog. This also allows you to modify the URL between redirections if, for example, you are taking this opportunity to change your permalink structure.

First, create a new subdomain. I called mine redirect.oxfordechoes.com. On Bluehost, this is done through the cPanel’s aptly named “Subdomains” tool. To make life simpler, point the subdomain to your main public_html directory, not a new subdirectory (as is the default).

Second, you need the PHP script to create the cookie and generate the redirect. Notice that the cookie is valid on '.oxfordechoes.com', so the main blog can see and react to it. Lines 4-5 update the permalink structure (from date-based to title-only) and can be removed (and $redir_uri changed to $request_uri on line 9) if you’re not making any changes. Obviously, change the hostname on lines 7 and 9 as appropriate.

Third, you need to ensure requests to redirect.yourhostname.com arrive at that PHP script. Add an equivalent of lines 4-5 below to your .htaccess file:

Finally, you need to take advantage of the newly set cookie to display an appropriate message on your website. I used this somewhat hectic code within a PHP Code widget, which works well. You can visit this blog’s old address to see it in action. Line 2 ensures the message is only displayed if the cookie is set, while lines 3-4 fade the message away and un-set the cookie when the user acknowledges the message.

Posted 12 Feb 2014 by John McManigle in Technical

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">