Skip to content

Instantly share code, notes, and snippets.

@kingkool68
Last active January 20, 2026 17:37
Show Gist options
  • Select an option

  • Save kingkool68/d5e483528a260e5c7921afb5c88bffd6 to your computer and use it in GitHub Desktop.

Select an option

Save kingkool68/d5e483528a260e5c7921afb5c88bffd6 to your computer and use it in GitHub Desktop.

Revisions

  1. kingkool68 revised this gist May 20, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion use-remote-media.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    if ( defined( 'RH_USE_REMOTE_MEDIA_URL' ) && ! empty( RH_USE_REMOTE_MEDIA_URL ) ) {
    add_filter( 'wp_get_attachment_image_src', 'filter_wp_get_attachment_image_src' );
    add_filter( 'wp_calculate_image_srcset', 'filter_wp_calculate_image_srcset' );
    add_filter( 'wp_get_attachment_url', $this, 'filter_wp_get_attachment_url' );
    add_filter( 'wp_get_attachment_url', 'filter_wp_get_attachment_url' );
    }

    function filter_wp_get_attachment_image_src( $image = array() ) {
  2. kingkool68 revised this gist May 12, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions use-remote-media.php
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@

    // Put the rest of this in functions.php or a custom plugin or somewhere else.
    if ( defined( 'RH_USE_REMOTE_MEDIA_URL' ) && ! empty( RH_USE_REMOTE_MEDIA_URL ) ) {
    add_filter( 'wp_get_attachment_image_src', array( $this, 'filter_wp_get_attachment_image_src' ) );
    add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_wp_calculate_image_srcset' ) );
    add_filter( 'wp_get_attachment_url', array( $this, 'filter_wp_get_attachment_url' ) );
    add_filter( 'wp_get_attachment_image_src', 'filter_wp_get_attachment_image_src' );
    add_filter( 'wp_calculate_image_srcset', 'filter_wp_calculate_image_srcset' );
    add_filter( 'wp_get_attachment_url', $this, 'filter_wp_get_attachment_url' );
    }

    function filter_wp_get_attachment_image_src( $image = array() ) {
  3. kingkool68 revised this gist May 10, 2020. No changes.
  4. kingkool68 created this gist May 10, 2020.
    62 changes: 62 additions & 0 deletions use-remote-media.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php
    // Put this in wp-config.php and replace https://example.com/ with the URL of the production site.
    define( 'RH_USE_REMOTE_MEDIA_URL', 'https://example.com/' );

    // Put the rest of this in functions.php or a custom plugin or somewhere else.
    if ( defined( 'RH_USE_REMOTE_MEDIA_URL' ) && ! empty( RH_USE_REMOTE_MEDIA_URL ) ) {
    add_filter( 'wp_get_attachment_image_src', array( $this, 'filter_wp_get_attachment_image_src' ) );
    add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_wp_calculate_image_srcset' ) );
    add_filter( 'wp_get_attachment_url', array( $this, 'filter_wp_get_attachment_url' ) );
    }

    function filter_wp_get_attachment_image_src( $image = array() ) {
    if ( ! is_array( $image ) || empty( $image ) ) {
    return $image;
    }
    $wp_upload_dir = wp_upload_dir();
    $base_dir = $wp_upload_dir['basedir'];
    $base_url = $wp_upload_dir['baseurl'];
    $absolute_path = str_replace( $base_url, $base_dir, $image[0] );
    if ( file_exists( $absolute_path ) ) {
    return $image;
    }
    $find = get_site_url();
    $replace = RH_USE_REMOTE_MEDIA_URL;
    $image[0] = str_replace( $find, $replace, $image[0] );
    return $image;
    }

    function filter_wp_calculate_image_srcset( $src = array() ) {
    if ( is_array( $src ) && ! is_admin() ) {
    $wp_upload_dir = wp_upload_dir();
    $base_dir = $wp_upload_dir['basedir'];
    $base_url = $wp_upload_dir['baseurl'];
    $find = get_site_url();
    $replace = RH_USE_REMOTE_MEDIA_URL;
    foreach ( $src as $key => $val ) {
    $absolute_path = str_replace( $base_url, $base_dir, $val['url'] );
    if ( ! file_exists( $absolute_path ) ) {
    $val['url'] = str_replace( $find, $replace, $val['url'] );
    $src[ $key ] = $val;
    }
    }
    }
    return $src;
    }

    function filter_wp_get_attachment_url( $url = '' ) {
    if ( is_admin() ) {
    return $url;
    }

    $wp_upload_dir = wp_upload_dir();
    $base_dir = $wp_upload_dir['basedir'];
    $base_url = $wp_upload_dir['baseurl'];
    $find = get_site_url();
    $replace = RH_USE_REMOTE_MEDIA_URL;
    $absolute_path = str_replace( $base_url, $base_dir, $url );
    if ( ! file_exists( $absolute_path ) ) {
    $url = str_replace( $find, $replace, $url );
    }
    return $url;
    }