Home > Article > CMS Tutorial > How to get WordPress media library to recognize .pdf files
How to make WordPress media library recognize .pdf files?
WordPress’s Media Library only supports images, videos and audios by default. Sometimes these are not enough. The Media Library allows many types of uploaded files and requires more detailed classification. For example, pdf files
recommended: "wordpress tutorial"
Let the media library support pdf classification
This code from tutsplus can help us achieve the effect shown in the picture above. Put the code in the theme's functions.php
The code is as follows
function modify_post_mime_types( $post_mime_types ) { // 选择mime类型,这里用: 'application/pdf' // 然后扩充数组,定义label的文字 $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' );
Upload a pdf to the media library file, you can see the effect.
How to support more categories
The file types supported by WordPress are written in wp_includes/functions.php, search for it
The code is as follows:
function get_allowed_mime_types()
You can find these types
The code is as follows:
'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tif|tiff' => 'image/tiff', 'ico' => 'image/x-icon', 'asf|asx|wax|wmv|wmx' => 'video/asf', 'avi' => 'video/avi', 'divx' => 'video/divx', 'flv' => 'video/x-flv', ...
Find the type you need, follow the
code as follows:
$post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) );
, change 'application Just replace /pdf' with the required mime type, and the following text should be changed accordingly. This is the way to add array members in PHP. You can of course add more array elements to support multiple custom types.
The above is the detailed content of How to get WordPress media library to recognize .pdf files. For more information, please follow other related articles on the PHP Chinese website!