Skip to content

Instantly share code, notes, and snippets.

@beneverard
Last active December 29, 2015 08:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beneverard/7642928 to your computer and use it in GitHub Desktop.
Save beneverard/7642928 to your computer and use it in GitHub Desktop.
Rewrite attachment URLs from a local version of WordPress to a remote version.
// remove username:password if you don't need htpasswd auth
add_filter('wp_get_attachment_url', function($url) {
$upload_dir = wp_upload_dir();
$file_path = str_replace(site_url() . '/wp-content/uploads', $upload_dir['basedir'], $url);
// check to see if the file exists locally first
if ( file_exists($file_path) ) {
return $url;
}
return str_replace(site_url(), 'http://username:password@staging.domain.co.uk', $url);
});
@beneverard
Copy link
Author

Possible improvements, check if file exists locally... if not, try and load remote version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment