Change the default WordPress media MIME icons

I recently helped a someone at Stack Exchange with replacing the MIME icons used to identify the type of file for each media item. I decided to post it here for posterity’s sake. Here’s how you can add a little bit of spice to the WordPress admin panel.

This can be accomplished in three easy steps:

1. Create a directory in your theme that will hold all the new icons /themes/theme-name/images/icons/
2. Copy all the images from /wp-includes/images/crystal/ to your new directory
3. Hook into the wp_mime_type_icon filter in your functions.php file

function change_mime_icon($icon, $mime = null, $post_id = null){
    $icon = str_replace(get_bloginfo('wpurl').'/wp-includes/images/crystal/', WP_CONTENT_URL . '/themes/theme-name/images/icons/', $icon);
    return $icon;
add_filter('wp_mime_type_icon', 'change_mime_icon');

To verify this is working, open the media library and browse the media gallery. If you see PHP warnings or errors, somethings has gone awry. If you see the icons displayed, you now have control over the images as they are being pulled from your theme directory.

Be careful not to change the names of the image files as they are called specifically in the core.

Once again, I originally posted this on WordPress Answers hosted by Stack Exchange. This is for those who might assume plagiarizing shenanigans.