钩子文档

post_mime_types

💡 云策文档标注

概述

post_mime_types 是一个 WordPress 过滤器,用于修改默认的文章 MIME 类型列表。它允许开发者自定义媒体库中文件类型的过滤选项,例如重命名或移除特定类型。

关键要点

  • post_mime_types 过滤器用于过滤默认的文章 MIME 类型列表,参数为 $post_mime_types 数组。
  • 通过 add_filter 钩子可以添加自定义函数来修改 MIME 类型,例如重命名或移除媒体库下拉菜单中的项目。
  • 此过滤器在 WordPress 2.5.0 版本中引入,相关函数包括 get_post_mime_types()。

代码示例

function edit_post_mime_types($post_mime_types){
    $post_mime_types['application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-word.document.macroEnabled.12,application/vnd.ms-word.template.macroEnabled.12,application/vnd.oasis.opendocument.text,application/vnd.apple.pages,application/pdf,application/vnd.ms-xpsdocument,application/oxps,application/rtf,application/wordperfect,application/octet-stream'][0] = 'PDFs';   // Change Documents to PDFs only; 
    unset($post_mime_types['audio']); // Remove Audios
    unset($post_mime_types['video']); // Remove Videos
    unset($post_mime_types['application/x-gzip,application/rar,application/x-tar,application/zip,application/x-7z-compressed']); // Remove Archives
    unset($post_mime_types['application/vnd.apple.numbers,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel.sheet.macroEnabled.12,application/vnd.ms-excel.sheet.binary.macroEnabled.12']); // Remove Spreadsheets

    return $post_mime_types;
}

add_filter('post_mime_types', 'edit_post_mime_types');

注意事项

使用此过滤器可以修改媒体库下拉菜单中的 MIME 类型显示,但需注意 upload_mimes 过滤器可能无法完全移除这些项目,因此 post_mime_types 过滤器在此场景下更有效。


📄 原文内容

Filters the default list of post mime types.

Parameters

$post_mime_typesarray
Default list of post mime types.

Source

return apply_filters( 'post_mime_types', $post_mime_types );

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    To unset and rename some items in the dropdown filter for media items:

    function edit_post_mime_types($post_mime_types){
        $post_mime_types['application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-word.document.macroEnabled.12,application/vnd.ms-word.template.macroEnabled.12,application/vnd.oasis.opendocument.text,application/vnd.apple.pages,application/pdf,application/vnd.ms-xpsdocument,application/oxps,application/rtf,application/wordperfect,application/octet-stream'][0] = 'PDFs';   // Change Documents to PDFs only; 
        unset($post_mime_types['audio']); // Remove Audios
        unset($post_mime_types['video']); // Remove Videos
        unset($post_mime_types['application/x-gzip,application/rar,application/x-tar,application/zip,application/x-7z-compressed']); // Remove Archives
        unset($post_mime_types['application/vnd.apple.numbers,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel.sheet.macroEnabled.12,application/vnd.ms-excel.sheet.binary.macroEnabled.12']); // Remove Spreadsheets
    
        return $post_mime_types;
    }
    
    add_filter('post_mime_types', 'edit_post_mime_types');

    I had to resort to this because apparently, restricting all these file types using upload_mimes filter still doesn’t remove these items in the dropdown.