Skip to content

Instantly share code, notes, and snippets.

@CurtisL
Last active December 14, 2015 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CurtisL/ccac465e52061383c88b to your computer and use it in GitHub Desktop.
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.
<?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' );
@CurtisL
Copy link
Author

CurtisL commented Dec 8, 2015

old file_exists() check was improperly checking a relative path. Prepending the ABSPATH 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.

@CurtisL
Copy link
Author

CurtisL commented Dec 14, 2015

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