Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of letting WordPress Media Library recognize .pdf Files

Shulou Source: shulou.com Published: 2022-06-02 05:05:20 09月14日 Update

Editor to share with you how to let the WordPress media library identify .pdf files, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

How do I get the WordPress library to recognize .pdf files?

WordPress's media library (Media Library) only supports pictures, videos and audio by default. Sometimes these are not enough. The media library allows a wide variety of files to be uploaded, and more detailed categories are needed, such as pdf files.

Let the media library support pdf classification

This code from tutsplus can help us achieve the effect shown above by putting the code into the functions.php of the theme

The code is as follows

Function modify_post_mime_types ($post_mime_types) {/ / Select mime type, here use: 'application/pdf' / / then expand the array, define the text $post_mime_types [' application/pdf'] = array (_ ('PDFs'), _ (' Manage PDFs'), _ n_noop ('PDF (% s)', 'PDFs (% s)') of label) / / 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 library and you can see the effect.

How to support more classifications

The file types supported by WordPress are written in wp_includes/functions.php. Search for them.

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

The code is as follows:

$post_mime_types ['application/pdf'] = array (_ (' PDFs'), _ ('Manage PDFs'), _ n_noop (' PDF (% s)', 'PDFs (% s)'))

Replace 'application/pdf'' with the desired mime type, and the following text should be changed accordingly. This is the way to add array members in php, and of course you can add more group elements to support multiple custom types.

The above is all the contents of the method to let the WordPress Media Library recognize .pdf files. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Media files types code support more categories methods content effects arrays text articles help picture above not enough not much themes elements writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS vpn MySQL MariaDB