2018.3.08 让WordPress支持pdf文件的上传

让WordPress支持pdf文件的上传,将下面的代码放到主题的functions.php中
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’ );
可以看效果了。
让WordPress支持更多类型文件的上传
WordPress支持的文件类型在wp_includes/functions.php中有写,搜索一下
function get_allowed_mime_types()
可以找到以下类型:
‘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’,
…
插入自己需要的类型,按照下面代码进行编写:
$post_mime_types[‘application/pdf’] = array( __( ’PDFs’ ), __( ’Manage PDFs’ ),
_n_noop( ’PDF (%s)’, ’PDFs (%s)’ ) );
将‘application/pdf’替换成你所需要的文件类型,后面的文字也要相应改一改。