WordPress的媒体库默认只支持图片、视频和音频,有时候这些是不够用的,媒体库允许上传的文件种类众多,需要更细化的分类,比如pdf文件,今日就分享WordPress媒体库识别.pdf文件方法。

让媒体库支持pdf分类

这段来自tutsplus的代码可以帮助我们实现,将代码放到WordPress主题的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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ );

到媒体库中上传一个pdf文件,就可以看到效果了。

如何支持更多分类

WordPress支持的文件类型在wp_includes/functions.php中有写,搜索一下

代码如下:

function get_allowed_mime_types()

就可以找到这些类型

代码如下:

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’jpg|jpeg|jpe\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’image/jpeg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’gif\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’image/gif\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’jpg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’image/jpg\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’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’替换成需要的mime类型即可,后面的文字也要相应改一改。这是php中增加数组成员的方式,你当然可以增加更多数组元素实现支持多个自定义类型。

以上就是WordPress媒体库识别.pdf文件方法的详细内容,更多请关注WordPress主题站。

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部