How to use live images on a local WP install? I want to do something like the code down here in the wp-config.php
. Problem is that the siteurl
must be a relative path and cant be a url. I want to set up a local environment to test some parts offline and need to show the images.
if ($_SERVER['SERVER_ADMIN'] == 'dev') {
define('WP_HOME','http://localhost/siteurl.com/public_html/');
define('WP_SITEURL','http://localhost/siteurl.com/public_html/');
// use live images
define( 'UPLOADS', 'http://siteurl.com/wp-content/uploads/' );
}
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.