PHP warnings when processing 404 images in post content
-
We encountered PHP warnings when the plugin processes missing/404 images embedded in post content:
PHP Warning: file_get_contents(https://example.com/images/missing.jpg):
Failed to open stream: HTTP request failed! HTTP/1.1 404 Not FoundPHP Warning: exif_imagetype(https://example.com/wp-content/uploads/missing.gif):
Failed to open stream: HTTP request failed! HTTP/1.1 404 Not FoundIssue: The
saswp_get_image_details()function attempts to validate all images in content, including external or broken URLs, causing error log spam.Workaround: We added a filter to only process images from our uploads directory:
add_filter('saswp_get_image_details', function($img_details, $url) {
if ($img_details !== false) return $img_details;
if (!filter_var($url, FILTER_VALIDATE_URL)) return false;
$uploads_url = wp_upload_dir()['baseurl'];
$uploads_normalized = preg_replace('#^https?://#', '', $uploads_url);
$url_normalized = preg_replace('#^https?://#', '', $url);
return (strpos($url_normalized, $uploads_normalized) === 0) ? false : [];
}, 10, 2);Suggestion: Consider adding URL validation before calling file_get_contents() or gracefully handling 404 responses to prevent warnings on missing images.
Thanks!
You must be logged in to reply to this topic.