I'm looking for a way to scrape an external page for its first/best/largest image and insert a copy of that image as the featured image of a new post on my Wordpress site.

The way that I was hoping this would work would be that a user of my site would enter the URL of an external webpage into a form, and it would scrape that page for its first/best/largest image and set it as the featured image of a new post, which would then be published upon submission of the form. I have the front-end post submission set up with Gravity Forms which allows users of my site to create new posts, but I am confused with the backend web scraping part of it.

Is it possible to achieve this in Wordpress? Maybe using the HTTP API? I'm not a programmer so the HTTP API is a little intimidating to me. I'm hoping someone here with more programming knowledge than me that is familiar with Wordpress can help.

  • scrape URL for its first image? Do you mean wp post's first image? There are several answers to that on stackexchange. – Wali Hassan May 16 '15 at 22:35
  • Sorry, I fixed my question to be clearer on what I am asking. – 5AMWE5T May 16 '15 at 23:59

It is possible as referenced here

but you are talking about a great deal of customization in the Gravity forms plugin and like you said you are not a programmer yourself, so you better open a support ticket in their forums and they might be able to help.

Also look at this code that can help you if you decide to do it yourself

    $upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
    $file = $upload_dir['path'] . '/' . $filename;
    $file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);

$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => sanitize_file_name($filename),
    'post_content' => '',
    'post_status' => 'inherit'
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail( $post_id, $attach_id );

$image_url is what you will get from user input / upload.

  • Is there a way to scrape an image from a page where the URL isn't of the image? Like a blog post with an image for example? I will follow your advice and open a support ticket at the Gravity Forms website. – 5AMWE5T May 17 '15 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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