When I first began looking into this, File Gallery got me much closer to solving the problem. The only issue is that the plugin only displays the generic non-image icon when I upload a file.
Does anyone know of a pre-existing plugin that would either work alongside File Gallery or in place of it to achieve logos specific to a file type (say, I upload a .doc file then the plugin displays a MS Word logo).
Failing a pre-existing plugin, what would be a good starting point to attempt to craft my own hand-made solution?
Thanks in advance!
Note: This was originally posted in on the wordpress.org support forum here, but since it got no responses there, I thought I’d try my luck here.
2 Answers
WordPress has a native function wp_mime_type_icon()
in wp-includes/post.php
that you can use.
Basic example:
// $attachment should be a full post object
if ( wp_attachment_is_image( $attachment->ID ) )
{
echo wp_get_attachment_image(
$attachment->ID,
array( 480, 900 ),
FALSE,
array ( 'class' => 'aligncenter' )
);
}
else
{
echo '<img src="' . wp_mime_type_icon( $attachment->post_mime_type ) . '">';
}
Look in wp-includes/images/crystal/
for available file type icons:
- archive
- audio
- code
- default
- document
- interactive
- spreadsheet
- text
- video
You can set up your own image directory and filter 'icon_dir'
for the local path and 'icon_dir_uri'
for the URIs to let WordPress use your images.
To change just singular files filter 'wp_mime_type_icon'
:
apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id )