WordPress XML Media Import with HTTP Authentication

I’m trying to use the wordpress-importer plugin to import an xml file exported from another wordpress blog which happens to have HTTP authentication on it.

As is, when I run the import, media files are failing with:

Read More
Failed to import Media “Image replace”: Remote server returned error response 401 Unauthorized

If I do a find and replace in the XML file on the URI to be

username:password@blogtoimportfrom.com

I get

Failed to import Media “Image replace”: Remote server did not respond

Should I be more selective with my find / replace? Or is there some other way to provide the Auth credentials?

TIA,
Billy

Related posts

Leave a Reply

2 comments

  1. I was able to get this to work. I did a find and replace on all occurrences of the URI with the username / password version.

    I then had to modify one line of WordPress code. In

    wp-includes/http.php
    

    I changed this method:

    function wp_safe_remote_request( $url, $args = array() ) {
            $args['reject_unsafe_urls'] = true;
            $http = _wp_http_get_object();
            return $http->request( $url, $args );
    }
    

    to read

    function wp_safe_remote_request( $url, $args = array() ) {
            $args['reject_unsafe_urls'] = false;   // <------- just this line
            $http = _wp_http_get_object();
            return $http->request( $url, $args );
    }
    
  2. I had to use headers to auth because wp was mangling the auth in the url.

    In wordpress-importer.php…

    $args_ = array(
                'timeout' => 300,
                'stream' => true,
                'filename' => $upload['file'],
                'headers' => array('Authorization' => 'Basic ' . base64_encode( 'user:pass'))
        );
    
    $remote_response = wp_safe_remote_get( $url,  $args_);