Home >Backend Development >PHP Tutorial >Quick Tip: Enhance WordPress Media Manager with Additional Media Type Filters
By default, the WordPress Media Manager only allows you to filter media by three types; images, video, and audio.
The website I am currently developing will use a lot of PDF attachments, so I would like to add PDF filtering functionality to the media manager.
I discovered that additional file type filtering can be added to the media manager using a simple filter hook.
To achieve this we will use the "post_mime_types
" filter.
In our function, we will use the mime type slug to select the file type, the slug for PDF is 'application/pdf
'
Then we define an array containing the text labels of the file types.
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 PDF files will now appear in your media manager (as long as you have at least one PDF in your media library)
You can do this on any file supported by WordPress. Supported file types are defined by WordPress in wp-includes/functions.php
The default supported file types are defined in the get_allowed_mime_types()
function.
To find the slug for the file type you are looking for, just search for "get_allowed_mime_types()" in
wp-includes/functions.php
The number of file types supported by WordPress is quite extensive, so I won’t list them all, but here’s a small example:
'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 slug for the Flash .swf file will be "application/x-shockwave-flash
".
Do you have any suggestions regarding file types in WordPress Media Manager? Share them in the comments below!
The above is the detailed content of Quick Tip: Enhance WordPress Media Manager with Additional Media Type Filters. For more information, please follow other related articles on the PHP Chinese website!