Home> CMS Tutorial> WordPress> body text

How to get WordPress media library to recognize .pdf files

藏色散人
Release: 2020-01-02 09:50:57
Original
2188 people have browsed it

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 (%s)', 'PDFs (%s)' ) ); // then we return the $post_mime_types variable return $post_mime_types; } // Add Filter Hook add_filter( 'post_mime_types', 'modify_post_mime_types' );
Copy after login

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()
Copy after login

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', ...
Copy after login

Find the type you need, follow the

code as follows:

$post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF (%s)', 'PDFs (%s)' ) );
Copy after login

, 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!