How to make WordPress media library recognize. PDF files

Time:2019-12-18

Let the media library support PDF classification
This code from tutsplus can help us achieve the effect shown in the figure above, and put the code into functions.php of the topic

Copy code

The code is as follows:

function modify_post_mime_types( $post_mime_types ) {
//Select the MIME type and use: ‘application / PDF’
//Then expand the array to define the text of the label
$post_mime_types[‘application/pdf‘] = array( __( ‘PDFs’ ), __( ‘Manage PDFs’ ),
_n_noop( ‘PDF <span>(%s)</span>’, ‘PDFs <span>(%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 file to the media library to see the effect.

How to support more classifications
The file types supported by WordPress are written in WP? Includes / functions.php. Search

Copy code

The code is as follows:

function get_allowed_mime_types()

You can find these types

Copy code

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 and follow the

Copy code

The code is as follows:

$post_mime_types[‘application/pdf’] = array( __( ‘PDFs’ ), __( ‘Manage PDFs’ ),
_n_noop( ‘PDF <span>(%s)</span>’, ‘PDFs <span>(%s)</span>’ ) );

The writing method of “application / PDF” can be replaced by the required MIME type, and the following text should be changed accordingly. This is the way to add array members in PHP. Of course, you can add more array elements to support multiple custom types.