Home  >  Article  >  CMS Tutorial  >  How to get WordPress media library to recognize .pdf files

How to get WordPress media library to recognize .pdf files

藏色散人
藏色散人Original
2020-01-02 09:48:152260browse

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( &#39;PDF <span class="count">(%s)</span>&#39;, &#39;PDFs <span class="count">(%s)</span>&#39; ) ); 
// then we return the $post_mime_types variable 
return $post_mime_types; 
} 
// Add Filter Hook 
add_filter( &#39;post_mime_types&#39;, &#39;modify_post_mime_types&#39; );

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:

&#39;jpg|jpeg|jpe&#39; => &#39;image/jpeg&#39;, 
&#39;gif&#39; => &#39;image/gif&#39;, 
&#39;png&#39; => &#39;image/png&#39;, 
&#39;bmp&#39; => &#39;image/bmp&#39;, 
&#39;tif|tiff&#39; => &#39;image/tiff&#39;, 
&#39;ico&#39; => &#39;image/x-icon&#39;, 
&#39;asf|asx|wax|wmv|wmx&#39; => &#39;video/asf&#39;, 
&#39;avi&#39; => &#39;video/avi&#39;, 
&#39;divx&#39; => &#39;video/divx&#39;, 
&#39;flv&#39; => &#39;video/x-flv&#39;, 
...

Find the type you need, follow the

code as follows:

$post_mime_types[&#39;application/pdf&#39;] = array( __( &#39;PDFs&#39; ), __( &#39;Manage PDFs&#39; ), 
_n_noop( &#39;PDF <span class="count">(%s)</span>&#39;, &#39;PDFs <span class="count">(%s)</span>&#39; ) );

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

Statement:
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