I figured out a solution! Not that it explains some of the weird str_replace activity that I was seeing, so if anyone can explain that please do.
But to get around the original issue I was experiencing – The line
$filepath = str_replace($site_url."https://wordpress.stackexchange.com/", '', $attachment->image_src_large[0]);
was only having an effect on either back or front end but not both because $attachment->image_src_large[0]
was different for each. On front end I would get https://...
but on back end, for whatever reason, I would get http://...
.
So I expanded $site_url = get_site_url();
to be:
$site_url = get_site_url();
if (strpos($site_url, 'https') !== false){
$ssl_site_url = $site_url;
$plain_site_url = str_replace("https", "http", $site_url);
} else {
$plain_site_url = $site_url;
$ssl_site_url = str_replace("http", "https", $site_url);
}
and then did a str_replace()
for both strings. Might be overkill but it will work no matter what protocol $attachment->image_src_large[0]
turns out to be.