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:
- 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.
- 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 URLbe_media_from_production_start_month
– Specify the Start Monthbe_media_from_production_start_year
– Specify the Start Yearbe_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.
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.
Chris says
Looks like this could be really useful.
Thank you for releasing it.
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.
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.
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.
Miguel Fdez says
Bill, thanks for the plugin!
You should update the readme.md file (“Requires at least: 3.9”), because “wp_get_attachment_image_src” filter was introduced in WP version 4.3.
https://core.trac.wordpress.org/browser/tags/4.5/src/wp-includes/media.php#L770
It drives me crazy to understand why the plugin was doing nothing in my installation… Finally I got it… My WP was v4.2.8
Bill Erickson says
Thanks! I didn’t realize that filter wasn’t available until 4.3. I’ve updated the readme: https://github.com/billerickson/BE-Media-from-Production/commit/a5541e99437ad1f46900cee3c8cc2bf13705510a
Victor Ramirez says
Wow. Just found this on your WPengine Git article. Going to save me 30GB on my hard drive.