Use either the search-replace command of wp-cli or the plugin “better search replace” to search for http://oldsite.com and replace with http://stagingsite.com.
Can you put a filter on any calls to image URLs? So you’d filter out whatever part of the URL refers to an online server and replace it with local://mysite.staging.com or something similar? Using str_replace might work: https://www.php.net/manual/en/function.str-replace.php
So something like:
$onlineimageurl = "http://xxxyyy.cloudfront.net/images/";
$localimageurl = "local://mysite.staging.com/images";
$wordpressimagelocal = str_replace ($onlineimageurl, $localimageurl, $wordpressimageonline);
Now $wordpressimageonline spits out a URL with the first part of it replaced to point to your local copy. I think?
Or you could do a find a replace in your local database using this same logic: find the online beginning URL of all images and replace that URL with your local URL. WordPress has some find and replace plugins, though if you can manage this on your local database that might be easier. That would change all image URLs in the database to point to a local copy instead of the online one.
Keep us posted!
@tsguitar A DB find and replace not done with a serialization-aware parser will break the DB. Please do not suggest direct SQL search and replaces.
Oh yikes! I’ve done that kind of find and replace in the past without any trouble. In fact, it was nearly this exact kind of situation where I needed to find old URLs and replace them with new ones.
So maybe replacing the URL through a filter after it’s called out of the DB is better?
https://codex.wordpress.org/Function_Reference/maybe_serialize
In some cases WordPress can serialize the data, as it also matters how long byte it creates problems with a raw search and replacement.
Example s:20 indicates a string 20 bytes long, what happens if I change an old path to the new one but that is different from 20 bytes? Simply the database breaks.
The code statement should not be used without knowing what to do, use plugins that deal with the serialization of objects.