Last active
December 14, 2015 22:46
-
-
Save CurtisL/ccac465e52061383c88b to your computer and use it in GitHub Desktop.
Wordpress filter to replace 404'd images with a placeholder, Good for when you're working locally but don't want to copy down the whole uploads directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Dev Helper for 404'd Images | |
*/ | |
function placehold_404( $image ) { | |
if ( file_exists( ABSPATH . str_replace(get_home_url().'/', '', $image[0]) ) ) { | |
return $image; | |
} | |
// Replace the image source with a placeholder at the proper dimensions. | |
$image[0] = "http://placehold.it/{$image[1]}x{$image[2]}/FF6600/FFFFFF"; | |
return $image; | |
} | |
add_filter( 'wp_get_attachment_image_src', 'placehold_404' ); |
Breaks in WP 4.4 due to the addition of image srcset
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
old
file_exists()
check was improperly checking a relative path. Prepending theABSPATH
fixes this check. I didn't notice at first since the site I was originally working on locally was an image heavy site that had had thousands of images. So it replaced all images without me noticing. This new check should actually check if file exists if you have a partial uploads directory.