Plugin: Load Media from Production

I’ve worked on websites with absolutely massive uploads directories. duPont Registry’s Autofluence blog weighed in at 45GB, and Western Journalism was much larger still (I don’t know exactly how large, I didn’t attempt downloading it all).

Just setting up a development environment could take days, and that’s before I’ve started any actual work on the project. I don’t actually need a copy of all the uploads since I’ll be manually deploying the theme and plugin changes to their staging server. I just didn’t want broken images when my client reviewed the development site.

I came up with a relatively straightforward solution:

  1. Copy over a few months worth of uploads. This way I can create new image sizes and the first few pages of archives will look right.
  2. Filter the Image URLs. For any of the images I didn’t move over, use the production site’s URL so that they still load.

I have packaged this up as a plugin: BE Media from Production.

The simplest method is to set the start month and year of the oldest upload directory you carried over. The plugin will automatically create the list of all directories following that. Alternatively, you can manually provide a list of directories to include.

Available Filters:

  • be_media_from_production_url – Specify the Production URL
  • be_media_from_production_start_month – Specify the Start Month
  • be_media_from_production_start_year – Specify the Start Year
  • be_media_from_production_directories – Manually set the upload directories to use

Installation

Option 1: In your theme or core functionality plugin, specify the Production URL, Start Month and End Month using the provided filters. Example

Option 2: In your theme or core functionality plugin, specify the Production URL and specific directories using the provided filters. Example

This only affects images generated by WordPress functions like get_the_post_thumbnail() and wp_get_attachment_image(). Any hardcoded image URLs in post content will not be modified. You can use a tool like Search Replace DB 3 to update those URLs.

Release

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments

  1. Craig says

    Bill, if this works well (I’m optimistic – you do tremendous work), this will be an excellent resource for the WP community.

    Thank you!

    • Bill Erickson says

      Thanks! If it doesn’t work, let me know what issues you have. I’ve only used it on 3 or 4 projects so it hasn’t seen a ton of real-world testing.

      Here are a few situations where I know it won’t work:
      – If you don’t organize your uploads into year/month directories, but rather dump them all into a single directory.
      – If you’re uploading images using a plugin that puts it in a separate uploads directory, like a gallery plugin that creates a “gallery” directory in uploads.

  2. Puneet Sahalot says

    This is brilliant Bill!
    Exactly what I was looking for while building a national daily’s news website, we came across this issue of replicating the site and re-doing the theme.
    Will give it a try soon.
    Thanks for sharing.

  3. Patrick says

    Very cool. I’m working on a photographers blog project at the moment and wasn’t looking forward to the prospect of downloading the entire uploads folder. This should be very helpful.

  4. Damien Carbery says

    I am always trying to avoid externally provided files, so, could you add some apply_filters() calls to allow $directories, $start_month and $start_year be modified without modifying your code?

    • Bill Erickson says

      You’re right, I should have used filters from the start. I was thinking of this as a mu-plugin you’d have on dev only, and as such it would run before any other plugins/themes (so you wouldn’t be able to filter it).

      But by placing the filters in the functions as they are used, they run much later (init at the earliest) so can be utilized by a theme or plugin.

      I’ve updated the plugin documentation and the blog post above, as the filter approach is much cleaner.

      Thanks!

      • Damien Carbery says

        Of course, you’re right, it is a dev plugin so changing the plugin source would not have been such a big deal.
        That’s a nice bit of work to have nailed down so early in the morning.

  5. Victor Ramirez says

    Wow. Just found this on your WPengine Git article. Going to save me 30GB on my hard drive.

Leave A Reply