[wp-hackers] Page Templates based on MIME type (like image.php)

Glenn Ansley glenn at glennansley.com
Wed Jul 2 18:01:39 GMT 2008

Hi all,
I was looking through the core WP code, trying to understand how I could use
the theme-loader to hook redirect all images to a template specified in my
plugin much like the new image.php file in Kubrick does. I found some great
functionality that I haven't seen in the documentation so I thought I would
share it with the list.

It appears image.php isn't the only special file you can place in your theme
folder. WordPress will actually pick up any PHP file that corresponds to the
first part of your attachment's MIME type:
Therefore: placing the following files in your theme will give you a special
template-page just for that type of attachment

*File                  |  MIME Type*
image.php        |  image/jpeg, image/gif, etc
audio.php         |  audio/mpeg, etc
video.php         |  video/mpeg, etc
application.php | application/pdf, application/msword, etc

You can get a full list of MIME types (and potential theme template names)
at http://www.w3schools.com/media/media_mimeref.asp
You can also look in the posts table under the post_mime_type column if you
can't figure out what type of MIME your attachment is being logged as.
Also, there appears to be a list of accepted MIME types in
wp-includes/functions.php on line 1214 and following. This may limit the
type of XXXX.php template files you can create (but I think you can add to
the list with uploaded_mimes filter anyway)

So, any of the above templates will fire off if you click through to an
attachment page with corresponding MIME type and filename.php. Otherwise,
attachments.php will catch all attachment pages.
Great stuff. If this is common knowledge or I related incorrect info please
ignore/correct as needed.

Glenn Ansley

More information about the wp-hackers mailing list