WordPress Importer with remote lookup for assets.

Due to the lack of remote connectivity I did a small patch to the WordPress importer (v 0.61) that allows us to place the files locally and bypass the remote fetch the importer would usually attempt.

As I ran into this problem very frequently I think that might be useful for others as well.

In the plugins/wordpress-importer/wordpress-importer.php replace

$headers = wp_get_http( $url, $upload['file'] );

with

// fetch the remote url and write it to the placeholder
// Sometimes you already have all the files in place, so instead
// of downloading them again better see if you can reuse them
// put
//      define( 'WP_IMPORTER_CHECK_LOCAL_FILES', true );
// in you wp-config.php to do so.
$existing_upload = wp_upload_dir( $post['upload_date'] );
$existing_upload['file'] = $existing_upload['path'] . "/$file_name";
$existing_upload['url'] = $existing_upload['url'] . "/$file_name";
if ( defined( 'WP_IMPORTER_CHECK_LOCAL_FILES' ) && true === WP_IMPORTER_CHECK_LOCAL_FILES && file_exists( $existing_upload['file'] ) && filesize( $existing_upload['file'] ) > 0 ) {
    $headers = array(
        'response' => 200,
        'content-length' => filesize( $existing_upload['file'] ),
        'x-final-location' => $url
        );
    $upload = $existing_upload;
} else {
    $headers = wp_get_http( $url, $upload['file'] );
}

Then you can add define( 'WP_IMPORTER_CHECK_LOCAL_FILES', true ); in your config.php or functions.php file and the WordPress importer will try to locate the file in the uploads folder first and use it if it’s not with a zero filesize.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: