Unable to Load Images From Wordpress XML Exports When Importing

I had an interesting situation where I had used the export tool of Wordpress (via Tools > Export) to extract the data from an existing site ready to be imported into a new one as part of a migration.

As a consequence, when opening the XML file in notepad, it had a series of referenced image files of this form:

<wp:attachment\_url><!\[CDATA\[https://url.com/wp-content/uploads/2016/07/OJ\_Group\_02.jpg\]\]></wp:attachment\_url>

Straight-forward right? When Wordpress imports the file, it would go to those URLs and download the images before importing them straight back in again.

Since it is effectively a text file, if you don’t like where they are coming from, you can just do a search and replace and make the URL be something else.

Well, it should have been easy, but the problem I had was that the SSL certificate had expired so every time it went to get an image, it failed. That led to a situation where I had the posts, comments, pages etc. but no images. Drat.

Applying an SSL certificate to the original site wasn’t going to be easy - there were other apps on that host and I didn’t want to risk hurting them or causing any downtime, so what else could I do?

There was one easy alternative and one other option that I immediately thought of.

Let’s tackle the easy one first. If you can, just install something like Updraft. From here, you can export all the data into Zip files. Then, install the plugin on your destination site, and import them back in. Assuming you are able to manipulate plugins, that is by far the simplest approach provided you haven’t changed the name of your domain.

If you can’t and you want to stick with the in-built import process, you can do the following:

On your original site, take a copy of the images and place them somewhere externally visible. I chose the same destination host and used the html folder of Apache so that I could go to:

http:\\\[mydomain.com](http://mydomain.com/)\\file.jpg

To achieve this, let’s get the images first from your source Wordpress installation:

cd /var/www/folder-with-wordpress/wp-content/
tar cf uploads.tar uploads
gzip -9 uploads.tar

That will give you a zipped tar file with all of the images. Now ftp (or otherwise) them to the destination host and remove the file afterwards. For now, let’s say they are located within \tmp on our destination.

mkdir /var/www/html # if html folder doesn't exist....
gunzip uploads.tar
tar xf uploads.tar
cd uploads
cp -R * /var/www/html

Now, using the search and replace method above, you can change the location of the images by doing a search and replace using:

search: [https://url.com/wp-content/uploads/](https://url.com/wp-content/uploads/)
replace: [http://localhost/](http://localhost/)

Lastly, import the file in as before. What’s changed is that instead of going to the site with no certificate, it is pulling the data from itself, just in a different folder.

Don’t forget to free up any unneeded space.

cd /tmp
rm uploads.tar
rm -fr uploads

Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com