Jul 17, 2015

Get Images from Remote Server by Proxy

If you have the files and database you need to work on a WordPress site locally but not the images and other media, how can you get them from the remote server?

For small sites, simply downloading the entire wp-content/uploads/ directory may be the easiest and quickest solution. For large sites, however, itā€™s a much better idea to get the images on an as-needed basis. One way is to set up apache/nginx to load the media from the remote site for files that arenā€™t available locally. That method requires a little more knowledge and involves configuring files in your local web server though, so for this example weā€™re going to let a WordPress plugin do the hard work for us. Here are the steps:

Steps

  1. Download and activate the Uploads by Proxy plugin

  2. Open the siteā€™s wp-config.php file in an editor and add these lines of code, replacing ā€œexample.comā€ with whatever remote site you want to pull the images/media from:

define( 'UBP_IS_LOCAL', 'true' );
define( 'UBP_SITEURL', 'http://example.com' );

Tip: the main plugin page says that the define(ā€˜UBP_IS_LOCALā€™, ā€˜trueā€™); is only needed on staging servers for some reason, but I always have to enter that line to get it to work on local development sites, too.

Advantages

This method has two advantages over setting up an nginx proxy manually:

  1. Itā€™s quicker, since youā€™re simply activating a plugin and pasting in two lines of code; thereā€™s no time spent SSH-ing into your local nginx server and configuring files.

  2. It results in faster page loads, since this plugin actually downloads the images the first time they are accessed remotely via proxy, then for every subsequent request, they load much more quickly since you have them saved locally. By contrast, manually setting up apache/nginx to reference the remote images/media EVERY time rather than saving them locally.

Troubleshooting

Occasionally, I find that the plugin doesnā€™t work because itā€™s unable to get the IP address of the site you want to pull the images from. If that happens and youā€™re comfortable with the command line and editing PHP, you can run ping http://example.com, which will reveal the siteā€™s IP address, then paste it into the first line of the Uploads by Proxy get_ip() method, like this: return '12.34.567.890'. That will prevent the plugin from attempting to use third party services to look up the IP address of the remote site, and immediately return the correct IP address instead. Note that this change could get erased when Uploads by Proxy is updated, but you could always just paste it in again later to get it working again if that happens.