By default the WordPress Media Manager allows you to filter media by three types only; Images, Video and Audio.
The site I am currently developing will use a lot of PDF attachments, so I wanted to add PDF filtering to the media manager.
I found that extra file type filtering can be added to the media manager using a simple filter hook.
Example; Adding PDFs
To achieve this we are going to utilise the 'post_mime_types
' filter.
Within our function we will select the file type using the mime type slug, the slug for PDFs is 'application/pdf
'
Then we define an array that will contain the text labels for the file type.
function modify_post_mime_types( $post_mime_types ) { // select the mime type, here: 'application/pdf' // then we define an array with the label values $post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) ); // then we return the $post_mime_types variable return $post_mime_types; } // Add Filter Hook add_filter( 'post_mime_types', 'modify_post_mime_types' );
That's it! The option to filter out PDF files now appears in your Media Manager (once you have at least one PDF in your library)
Other File Types
You can do this with any files that WordPress supports. Supported file types are defined by WordPress in wp-includes/functions.php
The default supported file types are defined within the get_allowed_mime_types()
function.
To find the slug of the file type you're looking for just search for 'get_allowed_mime_types()
' in wp-includes/functions.php
The number of file types that WordPress supports is extensive so I won't list them all, but here's a small sample:
'pdf' => 'application/pdf', 'swf' => 'application/x-shockwave-flash', 'mov|qt' => 'video/quicktime', 'flv' => 'video/x-flv', 'js' => 'application/javascript', 'avi' => 'video/avi', 'divx' => 'video/divx',
As you can see the see the slug for Flash .swf files would be 'application/x-shockwave-flash
'.
Do you have any tips on file types in WordPress' media manager? Share them in the comments below!
Comments