0

I was wondering if there was a way to scrape an external webpage for its first/best image and add it to a new post in Wordpress. This seems like it would not be too complicated, but I can't seem to find any existing plugins that do anything like to this. I know there are plugins that can set an external image URL as the featured image, like WP Remote Thumbnail, but is there a way to scrape external webpages, like a blog post for example, for an image and add it to a new post in Wordpress?

1
  • Do you have a code with the solution to this? I also need this feature.
    – LTech
    Commented Jun 11, 2019 at 18:54

1 Answer 1

1

For all my answers I'm assuming you both have the legal rights to use those images and are aware of any implications of duplicate content (SEO etc). That being said there are definitely ways.

At what point would you like to scrape images? Regularly? If so look into WP_Cron and automate it.

If you just want to scrape the image once you generate a new post, you could add your custom code to the save_post hook. However you still need to specify which URL to scrape from. For this you could add a Custom Metabox with an URL field.

For the actual scraping of the page you should look into DOMDocument which makes handling a rendered HTML page with PHP pretty easy. You could use DOMDocument::getElementsByTagName to find an image tag. You could then use PHP's image function to check for a certain minimum size or other criteria that makes this image the "best" image.

Once you have the image you can upload it into the Wordpress media library using wp_handle_upload (I don't have enough reputation to post another link). The only thing left then is to set your image to the featured image. Again not enough reputation to post another link, but a quick Google search will lead to results on how to do that.

1
  • Thanks this is really useful and it will help me get started with this.
    – 5AMWE5T
    Commented May 25, 2015 at 4:14

Your Answer

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

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