Import Files into WordPress that aren’t in the Media Library

We are working on a new site for a client and decided that a fresh start would be the way to go. However, we wanted to go ahead and import all of their blog posts too. Unfortunately the WordPress importer doesn’t import images, etc. that are not set as a featured image. We went ahead and uploaded all of the /uploads/[YEAR]/[MONTH] assets into the new site, but we had one problem. None of those items were in the Media Library.

There are other plugins available that allow you to upload all your files into a folder on your server and then import those files into the Media Library. However, if you have existing references to those files within posts and you want the folder structure to remain the same as you need it, you’re out of luck. Well not anymore! We may package this in a plugin, but I’m not sure how common of a problem this is. So for now, here is the code:

15 Comments on “Import Files into WordPress that aren’t in the Media Library”

  1. Nice work, I just tested on a new site, but I found an issue. This function doesn’t import files located in the upload directory. Only inside the year/month structure.


    1. Yes, I believe there is an existing plugin that will do that. This one was more geared toward those that were copying from an existing site and needing to import through an existing upload structure. It can be tweaked for your use case I’m sure.

  2. I tried the plugin (Add from server) but it cannot handle high amounts of files and didn’t like the fact that it generates thumbs (too slow).
    I tried with your function and worked fine with the folders inside, but not with the files on the upload directory.
    I am not a php coder, but a .net coder, anyway I tried to tweak your function something like this to get the media items inside the upload directory and then run the loop for each media item:

    $media_items = glob($upload_dir . ‘/*.{jpeg,gif,png,jpg}’, GLOB_BRACE);

    Didn’t work. Seems like I am doing it wrong xD


  3. Hi Nick,
    This looked like just what I needed – added the code to the themes functions.php and ran but no luck – still can’t see the images in the media library in WP but can see them in File Manager and they work OK on the site.

    My problem is I can’t use them again and when I trued the Add from Server plugin it said the files were already present!

    Arrgghh – not expecting a solution just need to let off some steam!


  4. Just wanted to say thanks for this script! Saved a ton of time. Any suggestions to stop getting a 504 timeout when running it (guess we have too many images)?

    1. You could slim it down by limiting the number of loops of the years that you allow it to go through, for example on line 26 within the foreach loop for the years, you could add something like if(!in_array($yf, array(2016,2017))) { continue; } This will ensure that the script will only run for 2016 and 2017 folders. You could then limit further by month if need be within the for loop that is next.

  5. Just wanted to say thank you for this β€”Β it worked like a charm and much better than a couple of the plugins I tried.

  6. Thank you for writing this! I’m getting an error when executing the function with this error that I’m having some trouble resolving. I’d love to hear any ideas you might have on what I’ve done wrong. Thank you again for sharing your work!

    Fatal error: Uncaught Error: Call to undefined function wp_cache_get() in /home/blacka81/public_html/wp-includes/option.php:71 Stack trace: #0 /home/blacka81/public_html/wp-includes/functions.php(1927): get_option(‘siteurl’) #1 /home/blacka81/public_html/wp-includes/functions.php(1877): _wp_upload_dir(NULL) #2 /home/blacka81/public_html/wp-includes/functions.php(6057): wp_upload_dir() #3 /home/blacka81/public_html/wp-includes/functions.php(6049): import_non_existing_media_library_entries() #4 /home/blacka81/public_html/wp-settings.php(98): require(‘/home/blacka81/…’) #5 /home/blacka81/public_html/wp-config.php(91): require_once(‘/home/blacka81/…’) #6 /home/blacka81/public_html/wp-load.php(37): require_once(‘/home/blacka81/…’) #7 /home/blacka81/public_html/wp-blog-header.php(13): require_once(‘/home/blacka81/…’) #8 /home/blacka81/public_html/index.php(17): require(‘/home/blacka81/…’) #9 {main} thrown in /home/blacka81/public_html/wp-includes/option.php on line 71

Leave a Reply

Your email address will not be published. Required fields are marked *