Fix Missing Images During WordPress Database Import

After transferring a WordPress site from one domain to another there is often an issue with missing images. Even though you checked the box that sounds like it is downloading and transferring all attachments.

During the import process, you have that option to include all attachments. If you check this option, it will download all your images and upload them to the new site. While the images themselves HAVE been transferred, the code that references any images within your post will still be looking for the image at the old WordPress site. If you take a look at the code, you will see the image is looking for something like:

“OLD_DOMAIN.files.wordpress.com/2008/03/image_name.jpg”.

The image itself has been transferred to something like this:

“NEW_DOMAIN.com/wp-content/uploads/2008/03/image_name.jpg”.

You can test your site to make sure all the images are in fact on your server by copying that old image URL and replacing it with the correct new one and simply typing it into your address bar and hitting enter. If your post has a missing image and is looking for an image here:

“http://OLD_DOMAIN.files.wordpress.com/2008/03/image_name.jpg”

Then simply type in:

“http://NEW_DOMAIN.com/wp-content/uploads/2008/03/image_name.jpg”

If your image appears, then you are in good shape. The images have all transferred, they just need to be reconnected throughout the entire database.

The Solution

As you can see, it protected the last half of the address and kept “2008/03/image/name.jpg”. So all you have to do is edit that first half. If you have a couple posts, you can do it manually. But what if you have 100 or more posts and each one has a few images? Well, because WordPress runs on a MySQL database, you can run a quick SQL statement using PhpMyAdmin.

Here is the page from the Wordpress Codex on PhpMyAdmin.

This is a process that will work with the database directly and before doing anything like this, it is recommended that you first backup your database, in case something goes wrong. Here is a video tutorial with instructions on backing up a database and the page from the Wordpress Codex on backing up the database.

Once you have backed up your database, you want to run an SQL query which will find all mentions of one string of characters within every post and replace it with a new one. In this case we want to replace all mentions of the old image links with the new, correct one.

Here is a quick run-through of how to run an SQL query using PhpMyAdmin.

And here is the query you want to run if you moved from a wordpress.com site:

UPDATE wp_posts SET post_content = replace(post_content, ‘OLD_DOMAIN.files.wordpress.com/’, ‘NEW_DOMAIN.com/wp-content/uploads/’);

Follow the instructions in how to run an SQL query using PhpMyAdmin and type in the above EXACTLY, replacing ‘OLD_DOMAIN’ and ‘NEW_DOMAIN’ with the proper words. For example, if your old wordpress.com site was ‘myoldsite.wordpress.com’, the replace ‘OLD_DOMAIN’ with only ‘myoldsite’. If the new DOMAIN is ‘mynewsite.com’, then you replace ‘NEW_DOMAIN’ with ‘mynewsite’.

If you moved from another domain of your own, it will look more like this:

UPDATE wp_posts SET post_content = replace(post_content, ‘OLD_DOMAIN.com/wp-content/uploads/’, ‘NEW_DOMAIN.com/wp-content/uploads/’);

Hit ‘GO’  and watch the query run.

When the query has run, you will see the message at the top of the page letting you know it is complete. Go back to your site and all the images should be updated.

While you are in PhpMyAdmin, you can also run this to make sure that all links to posts using the old URL are switched to the new one:

UPDATE wp_posts SET post_content = replace(post_content, ‘http://OLD_DOMAIN.com’, ‘http://NEW_DOMAIN.com’);

Share and Enjoy:
  • Digg
  • del.icio.us
  • Design Float
  • DZone
  • StumbleUpon
  • TwitThis

This article is written by:

Name: Marty

URL: http://martythornley.com

Description: Filmmaker and Web Designer from Los Angeles California.

2 Responses

  1. Bryan P says:

    Its funny you post this. I am having the same problem, after recieving a Wordpress site from someone else, I acutally used Cpanel to export everything.

    But I have had the same problem.

  2. [...] Fix Missing Images During WordPress Database Import [...]