New Theme Feature: Mime Icons

WordPress 2.0 lets you upload files as post attachments. Every attachment has its own database record and its own web page much like its parent post. When linking to these attachments, you can use the URL of the file (a direct link) or the attachment’s page URL (sometimes called a subpost). These subposts display the title and description and a link to the file in a template that, by default, looks just like the post it’s attached to.

Very recently we added some new features that allow theme authors to customize the way attachments are displayed based on the type of file. An jpeg file will have the mime type “image/jpeg” while an mp3 will have the type “audio/mpeg”. When someone visits the attachment permalink, WordPress will look for theme files with names matching the mime type of the attached file. If the theme includes a file “audio.php” it will be used for all audio attachments. Likewise for a theme file named “image.php”. If the mime-related file isn’t found, WP will look for “attachment.php” and if that fails, it will fall into compatibility mode.

Compatibility mode ensures older themes will display attachments in WordPress 2.0. WordPress uses the theme’s usual template to display the subpost and adds a link to the attached file above the file’s description. This link is generated according to a set of rules: if it’s an image, use the thumbnail or the original; else if it’s another type of file and the theme includes an image named for the mime type use that; else make a text link using the attachment’s title.

That’s right, theme authors can now include graphic icons for attached files, e.g. audio.jpg, text.gif, application_msword.png! I think podcasting blogs will be the first to jump on this feature. Adventurous plugin authors are encouraged to find and use the many new and not-yet-documented hooks in these functions. It’s even possible to attach a thumbnail to any uploaded file, not just images, and have WP use that when generating the icon for that attachment.

ScreenshotsThe default theme has been updated to take advantage of most of the new attachment features listed above, as well as some more advanced programming to display the subposts differently depending on the size of the link generated. I put together a little demo showing this and I’ll explain it after the jump.

I hope you enjoy these features, or at the very least I hope they don’t get in your way if you aren’t using them!

Published by

Andy Skelton

Code Wrangler @ Automattic youtube.com/AndySkelton