Media Cleanup – Delete only image files

The Broadcast Media Cleanup add-on will normally delete all unused media it finds.

If you wish to only delete images (=ignore non-images), use the snippet below.

add_action( 'broadcast_media_cleanup_disqualify_media', 'my_broadcast_media_cleanup_disqualify_media' );
function my_broadcast_media_cleanup_disqualify_media( $action )
{
  foreach( $action->media_collection as $index => $media )
  {
    $guid = $media->get_guid();
    $type = wp_check_filetype( $guid, null );
    $type = $type[ 'type' ];

    // Is this type of an image type?
    $is_image = ( strpos( $type, 'image' ) === 0 );
    if ( $is_image )
      continue;
    $action->media_collection->forget( $index );
  }
}