How to use live images on local install?

Try to filter the output URLs temporarily to replace them with online images, using the following code:

add_filter( 'wp_get_attachment_image_src', function ( $image, $attachment_id, $size ) {

    // For thumbnails
    if ( $size ) {
        switch ( $size ) {
            case 'thumbnail':
            case 'medium':
            case 'medium-large':
            case 'large':
                $image[0] = str_replace( 'localhost/', 'EXAMPLE.COM/', $image[0] );
                break;
            default:
                break;
        }
    } else {
        $image[0] = str_replace( 'localhost/', 'EXAMPLE.COM/', $image[0] );
    }
    return $image;
}, 10, 3 );

This will replace any string containing localhost with your online domain’s name. However, you can’t modify or do anything with the image’s, it’s just for correcting the URL for development purposes.

Note that you should use the domain name without http:// or any / before the domain’s name.

Delete this function from your theme’s functions.php after you are done with it.

Leave a Comment