How to remove base url prepended to remote image url in wordpress

This is a php function you can use to remove base url prepended to image loaded from remote host.

Just copy this script and drop it in your theme function or plugin.

function removeWpBaseUrlFromRemoteImageSrcset( $sources, $size_array, $image_src, $image_meta, 
    $attachment_id ) 
        return $sources;

    $upload_dir    = wp_get_upload_dir();
    $image_baseurl = trailingslashit( $upload_dir['baseurl'] );
    $image_baseurl = set_url_scheme( $image_baseurl );

    foreach($sources as $key => $source){
        $url = $source['url'];

        $url = str_replace($image_baseurl, '', $url);

        if ( strpos($url, 'http') !== false || strpos($url, 'https') !== false) {
            $sources[$key]['url'] = $url;

    return $sources;

add_filter('wp_calculate_image_srcset', 'removeWpBaseUrlFromRemoteImageSrcset', 1000, 5);

Leave a Comment

Your email address will not be published. Required fields are marked *