Import all media referenced in posts

by Terence Eden   Last Updated August 29, 2017 13:08 PM

I want to import into the media library, all the images & videos referenced in my blog posts.

  1. Can I use get_attached_media() to get the images referenced on a page, even if they're not formally attached?
  2. Should I use media_handle_sideload() to insert the media into the library?


I've moved hosts, all my blog posts are available and all my images are in /wp-content/uploads/2001/08 etc.

I have around 13,000 images across ~120 subfolders (10 years of blogging ☺).

Here's what I've tried that hasn't worked:

  • Import WXR - imported the posts, but wouldn't import the images which were on the same server.
  • Media From FTP - didn't work with the huge number of images.
  • Add from Server - only works on a per-file basis. I couldn't select a whole folder and all its sub-folders.
  • Other plugins seem to suffer from the same issue - they don't deal with nested directories, and they can't cope with thousands of files.

All I want is something to scan my posts, find any <img src=", then import that into the media library - leaving the media in the same location. Attachment to the post & regeneration of thumbnails would be a bonus, but not essential.

Related Questions

Media import failed in the WordPress importer

Updated November 08, 2017 11:08 AM

Media Library empty after server migration

Updated September 13, 2017 05:08 AM