Skip to content

Instantly share code, notes, and snippets.

@tcrsavage
Created July 16, 2013 02:49
Show Gist options
  • Save tcrsavage/6005369 to your computer and use it in GitHub Desktop.
Save tcrsavage/6005369 to your computer and use it in GitHub Desktop.
Replace all broken attachment images with a default image - great for doing local dev with an imported database
add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment ) {
$content_path = ( defined( 'IMAGE_REPLACER_WP_CONTENT_PATH' ) ) ? IMAGE_REPLACER_WP_CONTENT_PATH : WP_CONTENT_DIR;
$content_url = ( defined( 'IMAGE_REPLACER_WP_CONTENT_URL' ) ) ? IMAGE_REPLACER_WP_CONTENT_URL : WP_CONTENT_URL;
$file_path = str_replace( $content_url, $content_path, $attr['src'] );
if ( file_exists( $file_path ) )
return $attr;
$attr['src'] = ( defined ( 'IMAGE_REPLACER_DEFAULT_IMAGE_URL' ) ) ? IMAGE_REPLACER_DEFAULT_IMAGE_URL : $content_url . '/default.png';
return $attr;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment