21

When I generate XML file with [Tools > Export] and use [Tools > Import] on another WordPress website I get "Failed to import Media" for each attachment post type. And images that failed to load are hotlinked from the previous server which consumes the transfer of that previous server...

But when I download "Unit Test" from http://codex.wordpress.org/Theme_Unit_Test then this problem doesn't exist. Some images still have "Failed to import Media" but it's only like 5 out of 30 images. They have URLs to their own server in their XML but somehow [Tools > Import] manages to download their media and replace these URLs with new locations (it is not hotlinked anymore).

What's the trick to make it work properly without "Failed to import Media"? Why even "Unit Test" has a few "Failed to import Media" messages?

7
  • As the server where you import needs to get them from the old location you should not only check if the files are at the old server, but also if the new server can fetch them. Maybe something limits outbound requests. Have you tried importing it on another server or localhost? I am afraid this could be a very localised issue.
    – kraftner
    Jun 6, 2014 at 15:49
  • 1
    As of WordPress 4.3.1 (possibly earlier) you can define IMPORT_DEBUG and set it to true to get meaningful feedback instead of just "Failed to import media". I can't imagine why this isn't default behavior. That may help narrow your problem down if the weather is just right.
    – Mark
    Oct 19, 2015 at 20:40
  • @Mark in which file can I set the IMPORT_DEBUGoption
    – kimliv
    Jun 11, 2016 at 9:34
  • 2
    @kimliv: you'll want to stick this in wp-config.php at the root of your WordPress install: define ( 'IMPORT_DEBUG', true );.
    – Mark
    Jun 12, 2016 at 12:40
  • 1
    Sorry for bumping, had the same issue and the original site from which I import had SSL not working and the xml URLs are on https, editing the xml files to toggle https to http helped.
    – Ismail
    Dec 10, 2016 at 17:15

10 Answers 10

21

I had a very similar problem when I moved a WordPress Blog from a single WordPress installation to a Multisite installation with different domain names but same IP.
I found out that the problem is the wp_http_validate_url function which drops the URL if the source IP is the same as the destination IP.
You can add a filter wp_http_validate_url to prevent this and allow matching source and destination IPs:

add_filter( 'http_request_host_is_external', '__return_true' );

Please see this answer for detailed explanation of the filter hook and why you should remove it after the import: https://wordpress.stackexchange.com/a/123313/75573

4
  • I spent close to 5 hours trying to import my posts with their images and this solved it.
    – Jonny
    Jun 19, 2019 at 16:54
  • I'm not sure I understand where this edit needs to go. Can someone explain further?
    – CXL
    Aug 23, 2019 at 2:39
  • You can drop it in functions.php or put it in a Plugin. The later makes it easier to turn it on and off as needed.
    – Merl
    Sep 10, 2019 at 10:33
  • Worked perfectly - Thank you! I just dropped it into a Code Snippets snippet May 9 at 13:30
2

Make sure your site isn't set to "Private".

When you export your Wordpress blog, all the textual content is embedded in the resulting XML file. All the binary content (pictures, file attachments etc) are exported as URLs pointing to their location on the existing blog. This is why when you import from a private Wordpress blog you will still be able to import all the posts/etc but all the images/etc will fail.

Here is where you need to change the setting as of the curent (2017-06-19) version:

enter image description here

Relevant Wordpress documentation:

https://en.support.wordpress.com/settings/privacy-settings/

Note that it needs to be done from your Wordpress.com account's settings, not from the settings of the blog itself, which is another potential point of confusion (and frustration).

1

When you generated your export file, was it on a live server or a localhost? Is that web page still accessable? If the media import fails, it means the images, as linked out of the XML, are coming up 404.

Open up the XML file and see where the images are pointing. If we open up the Theme Unit XML file, we will see lines like this:

<wp:attachment_url>http://wpthemetestdata.files.wordpress.com/2008/06/canola2.jpg</wp:attachment_url>

That file is very clearly available for import. What do your attachment_url files say?

4
  • 1
    I mentioned "And images that failed to load are hotlinked from the previous server which consumes the transfer of that previous server..." which indicates that images do not return 404, they are not on localhost but they still generate "Failed to import Media". My <wp:attachment_url> says example.com/2008/06/image.jpg too where example.com is always accessible domain. That's why I'm asking this question :) It clearly doesn't work even if images do not generate 404. It even doesn't work with wpthemetestdata.files.wordpress.com occasionally (~5 random images out of 30 fail).
    – Paul
    Oct 10, 2013 at 7:53
  • Right, sorry, my mind is a bit burned out at this point in the night. Do you have some kind of hotlink protection on the server? Oct 10, 2013 at 8:19
  • 1
    Nope, it's publicly accessible server with publicly accessible images, no protection whatsoever. But even if there was some protection, WordPress's server does the same thing occasionally. Does it mean that this importer is just buggy?
    – Paul
    Oct 10, 2013 at 8:35
  • That or one of the servers has an obnoxiously short timeout value. I'm fresh out of ideas for you. Does the XML import properly to Localhost? If so, you can sort of cheat... do the import locally then use WP Migrate DB and clone the result to live. Oct 10, 2013 at 8:57
1

I've been struggling with importing media to wordpress.org from wordpress.com as well. I managed to import all posts however images were still linked to the old site. I was nearly crying as I realised the only solution was to manually download and upload over 200 large images post by post (exporting/importing smaller portions of posts didn't work either).

After testing some plugins I found one that actually imports your wordpress media automatically. I couldn't believe but it actually does work. You have to go post by post but still it's better than manual download and upload. Here is the link if anyone is interested

1

This issue can happen when using certain plugins on the remote/local site.

Ideally, disable all plugins on both sites before exporting or importing, then reactivate them after the process is complete.

If you are trying to avoid disabling ALL plugins, then at least deactivate any plugins that affect images or image handling ... the most common type would probably be those that do image optimization.

In my case, I had EWWW Image Optimizer (which I highly recommend) active on the local site, and it caused this error. I disabled it, ran the import successfully, and reactivated it.

1

I finally figured it out.

Let's say we have domain1.com and domain2.com

Go to domain1.com -> tools -> export -> media

enter image description here

Now to go domain2.com -> tools -> import (upload xml file of media export)

enter image description here

Make sure you check download and import file attachments. enter image description here

This will take a while. While it's running, we will do the export of domain1.com pages or posts.

Open the xml file of either, and do a find and replace of domain1.com to domain2.com

enter image description here

Now if the other import finished, start the upload of that modified xml export, and guess what, it will now reference the uploaded images you did in the previous import.

0

I had the same problem and after hours of searching and testing the problem was that the source server was blocking image downloads because of "Hotlink Protection" being active in the cPanel.

1
  • You're right. In my case I realized that I implemented a hotlink protection in my .htaccess. Other cases are cPanel or Cloudflare.
    – John Doe
    Mar 22, 2019 at 22:10
0

For those that don't want to use add_filter or have no idea how to integrate it without creating a separate module. Here is a quick handy trick to get it done.

Open this file

vim ./wp-includes/http.php

Go to line 566 and comment out the following code block.

if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) )
  return false;

Once you comment those two lines out, go in and add the following line.

return true;

Go ahead and save the file and upload the XML file to try again. You will now download all the related content. Go back an remove the comment block and the return statement and you are good to go.

1
0

In my case I was trying to move a site from one hosing location to another. The new hosting had internally set the IP for the domain to its own server (obviously), after changing that to the old server the migrator knew where to look for the files.

0

I used wp-cli to import media from another site. wp import always complained Failed to import Media xxxx: Request failed due to an error: A valid URL was not provided. (http_request_failed) and imported nothing.

I enabled debug by adding define( 'IMPORT_DEBUG', true ); into wp-config.php and traced import call into deep. Here is the result:

#0  wp_validate_redirect / wp-includes/http.php:620
#1  allowed_http_request_hosts / wp-includes/class-wp-hook.php:294
#2  WP_Hook->apply_filters / wp-includes/plugin.php:212
#3  apply_filters / wp-includes/http.php:581
#4  wp_http_validate_url / wp-includes/class-http.php:270
#5  WP_Http->request / wp-includes/class-http.php:635
#6  WP_Http->get / wp-includes/http.php:68
#7  wp_safe_remote_get / wp-content/plugins/wordpress-importer/class-wp-import.php:1008
#8  WP_Import->fetch_remote_file / wp-content/plugins/wordpress-importer/class-wp-import.php:954
#9  WP_Import->process_attachment / wp-content/plugins/wordpress-importer/class-wp-import.php:697
#10 WP_Import->process_posts / wp-content/plugins/wordpress-importer/class-wp-import.php:88
#11 WP_Import->import / phar:.../Import_Command.php:179
#12 Import_Command->import_wxr / phar:.../Import_Command.php:81

Solution 1

Add following lines into wp-content/themes/{your theme}/functions.php:

add_filter("http_request_host_is_external", function($allow, $host, $url){
    if (gethostbyname($host) == "your_hostname.com") {
        $allow = true;
    }
    return $allow;
}, 10, 3);

It enables HTTP request to original site.

Solution 2

Admin panel / Settings / General / Site Address (URL) - here put URL of the original site. Or use wp-cli:

wp option update home your_hostname.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.