Ignoring that the RequestFromServerGlobal class isn’t available this is code that will grab media from wp-content/uploads and store it locally. This allows you to slowly build your local media library without 1 large sync.
It’s not the fastest if you have to grab dozens of images, but typically for local dev that’s fine.
add_action( 'plugins_loaded', function() {
try {
$request = new RequestFromServerGlobal( $_SERVER );
} catch ( \Exception $e ) {
return;
}
// If the request is for a file in wp-content/uploads on this domain, return the value of the prod image.
if ( str_starts_with( $request->get_uri(), '/wp-content/uploads/' ) ) {
// if the file exists, return that.
if ( file_exists( ABSPATH . $request->get_uri() ) ) {
return;
}
// Get the image from prod
$file = file_get_contents( 'https://www.prod-url.com' . $request->get_uri() );
if ( $file === false ) {
return;
}
// Save the image to the uploads directory
$dir = ABSPATH;
$filename = $dir . $request->get_uri();
$dir = dirname( $filename );
if ( ! file_exists( $dir ) ) {
mkdir( $dir, 0755, true );
}
file_put_contents( $filename, $file );
// Return the image
header( 'Content-Type: ' . mime_content_type( $filename ) );
readfile( $filename );
exit;
}
}, 10, 1 );
Linked From