WordPress: Import Images From Another Website

WordPressWithin WordPress, it’s actually extremely simple to grab images from another website, and import them into your own installation. It takes just a handful of functions to do so. This can be extremely handy when trying to move content into WordPress from another system.

For instance, on most organizational websites, you’ll find an employee directory; and in a lot of those directories you’ll find photographs of the employees. Should you ever choose to move your employee directory into WordPress, you can easily import the information as posts (most likely a custom post type) and then pull the employee images and add them to WordPress, too.

To grab the images, you’ll want to familiarize yourself with the WP_Http class. Then, you’ll use that class to request the image from the remote server. That might look something like:

if( !class_exists( 'WP_Http' ) )
	include_once( ABSPATH . WPINC. '/class-http.php' );

$photo = new WP_Http();
$photo = $photo->request( 'http://example.com/photos/directory/' . $photo_name . '.jpg' );

The binary code that makes up the image will be returned as the body of that request. Then, you’ll need to use the wp_upload_bits() function to save the file into your uploads directory. That might look something like:

$attachment = wp_upload_bits( $photo_name . '.jpg', null, $photo['body'], date("Y-m", strtotime( $photo['headers']['last-modified'] ) ) );

Next, you’ll want to store the image as an attachment in the database. To do so, you’ll first need to check the MIME type of the file, then use the wp_insert_attachment() function to add it to the database.

$filetype = wp_check_filetype( basename( $attachment['file'] ), null );

$postinfo = array(
	'post_mime_type'	=> $filetype['type'],
	'post_title'		=> $post->post_title . ' employee photograph',
	'post_content'	=> '',
	'post_status'	=> 'inherit',
);
$filename = $attachment['file'];
$attach_id = wp_insert_attachment( $postinfo, $filename, $postid );

Finally, you’ll need to generate and update the meta data for the image. To do so, you’ll use the wp_generate_attachment_metadata() and wp_update_attachment_metadata() functions.

if( !function_exists( 'wp_generate_attachment_data' ) )
	require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id,  $attach_data );

If you put it all together into a function, it might look something like:

function _import_photo( $postid, $photo_name ) {
	$post = get_post( $postid );
	if( empty( $post ) )
		return false;

	if( !class_exists( 'WP_Http' ) )
	  include_once( ABSPATH . WPINC. '/class-http.php' );

	$photo = new WP_Http();
	$photo = $photo->request( 'http://example.com/photos/directory/' . $photo_name . '.jpg' );
	if( $photo['response']['code'] != 200 )
		return false;

	$attachment = wp_upload_bits( $user_login . '.jpg', null, $photo['body'], date("Y-m", strtotime( $photo['headers']['last-modified'] ) ) );
	if( !empty( $attachment['error'] ) )
		return false;

	$filetype = wp_check_filetype( basename( $attachment['file'] ), null );

	$postinfo = array(
		'post_mime_type'	=> $filetype['type'],
		'post_title'		=> $post->post_title . ' employee photograph',
		'post_content'		=> '',
		'post_status'		=> 'inherit',
	);
	$filename = $attachment['file'];
	$attach_id = wp_insert_attachment( $postinfo, $filename, $postid );

	if( !function_exists( 'wp_generate_attachment_data' ) )
		require_once(ABSPATH . "wp-admin" . '/includes/image.php');
	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
	wp_update_attachment_metadata( $attach_id,  $attach_data );
	return $attach_id;
}

Important Note

These functions should only be used to import images that you have permission to use. This information should not be used to scrape images that belong to others.

6 Responses

  • Thomas

    Thanks, this really helped me a lot to improve aggregated content on my classifieds site!!

  • Dennis Smolek

    I rarely post comments on blogs but this article was EXTREMELY helpful to me. I’ve been a wordpress Dev for years and never knew the wp_http class or the wp_bits thing existed. thanks for posting.

  • Quentin Bolton

    Great post man. Minus a few tweaks, I’ve pretty much used your code straight up. Thank you for sharing it.

  • Fantastic post !

  • Harold

    Great post, resolved my problem, thank you!

  • Thanks, it works.

Post Your Comment

Your email address will not be published.