Skip to content

Instantly share code, notes, and snippets.

@noameppel
Created April 2, 2014 20:00
Show Gist options
  • Save noameppel/9941953 to your computer and use it in GitHub Desktop.
Save noameppel/9941953 to your computer and use it in GitHub Desktop.
Any media file not on development server will be loaded from live server.
<?php add_action('init', 'my_replace_image_urls' );
function my_replace_image_urls() {
if ( defined('WP_SITEURL') && defined('LIVE_SITEURL') ) {
if ( WP_SITEURL != LIVE_SITEURL ){
add_filter('wp_get_attachment_url', 'my_wp_get_attachment_url', 10, 2 );
}
}
}
function my_wp_get_attachment_url( $url, $post_id) {
if ( $file = get_post_meta( $post_id, '_wp_attached_file', true) ) {
if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
if ( file_exists( $uploads['basedir'] .'/'. $file ) ) {
return $url;
}
}
}
return str_replace( WP_SITEURL, LIVE_SITEURL, $url );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment