Browsed by
Tag: fix

How to fix 301 error when importing blog posts including images from a WordPress.com blog into a new WordPress.org blog

How to fix 301 error when importing blog posts including images from a WordPress.com blog into a new WordPress.org blog

Fourth of July night - Driving home

The current import script (as of WordPress 2.8.6) is broken when it comes to successfully importing images from WordPress.com. The error you see is something like

Remote file error: Remote file returned error response 301 Moved Permanently

Fixing this involves adding a couple of lines to a core WordPress file. Hopefully a future version of WordPress will include the working version.

Note that these instructions are for WordPress 2.8.6. Your version may be different, and you may need to play with this to get it to work for you. This worked for me, YMMV.

  1. Open wp-includes/functions.php
  2. Around line 1208 or so, you’ll find the wp_get_http function.
  3. Right below where it says $headers['response'] = $response['response']['code'];, add the following code (around line 1227):
    
    // added to fix 301 redirects for blog import code from WordPress.com
    if ((string)$response['response']['code'] == '301') {
    	$response = wp_remote_request($headers['location'], $options);
    	$headers = wp_remote_retrieve_headers($response);
    	$headers['response'] = $response['response']['code'];
    }
    
  4. Save the functions.php file and copy it back to the server.
  5. Re-run the import function (Tools > Import > WordPress). Don’t worry, it won’t make copies of the posts you’ve already imported, it will just download the images to your new blog.

To fix the references to the images so they’re being served off your new blog, you can either go through every post and manually correct them all, (not very fun), or better yet, download the Search and Replace plugin, activate it and do a search for all instances of the WordPress.com image server URL in all your posts (something like http://BLOGNAME.files.wordpress.com/ with your own new URL — http://BLOGNAME.com/wp-content/uploads/). Don’t forget to test the new URL structure before you do the search and replace, otherwise you’ll have to go back and fix it.

Hat-tip to Bill Zitomer for pointing out the link to this WordPress support forum page that had a good clue to the solution.