WordPress:允许上传媒体中的zip文件

arm*_*min 2 php media wordpress wordpress-gutenberg

我需要将 .zip 文件上传到媒体。

这里我尝试过

https://wordpress.stackexchange.com/questions/57603/is-it-possible-to-allow-zip-files-to-be-uploaded-in-wordpress

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
    // add your extension to the mimes array as below
    $existing_mimes['zip'] = 'application/zip';
    $existing_mimes['gz'] = 'application/x-gzip';
    return $existing_mimes;
}
Run Code Online (Sandbox Code Playgroud)

但它不适用于 Wordpress 5+ 或 gutenburg 编辑器

itz*_*kan 8

自 WordPress 4.7.1 或更高版本开始,它为 mime 类型添加了一些额外的安全检查。只需在活动主题的functions.php中添加以下代码片段即可实现上述功能 -

function modify_upload_mimes ( $mimes_types ) {
    // add your extension to the mimes array as below
    $mimes_types['zip'] = 'application/zip';
    $mimes_types['gz'] = 'application/x-gzip';
    return $mimes_types;
}
add_filter( 'upload_mimes', 'modify_upload_mimes', 99 );

function add_allow_upload_extension_exception( $types, $file, $filename, $mimes ) {
    // Do basic extension validation and MIME mapping
    $wp_filetype = wp_check_filetype( $filename, $mimes );
    $ext         = $wp_filetype['ext'];
    $type        = $wp_filetype['type'];
    if( in_array( $ext, array( 'zip', 'gz' ) ) ) { // it allows zip files
        $types['ext'] = $ext;
        $types['type'] = $type;
    }
    return $types;
}
add_filter( 'wp_check_filetype_and_ext', 'add_allow_upload_extension_exception', 99, 4 );
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请访问我的博客链接


小智 5

无需编辑文件!您可以在网络设置中编辑允许的扩展名并添加 zip。 我的网站 > 网络管理 > 设置 > 上传文件类型

或者

function zip_upload_mimes($existing_mimes = array()) {
    $existing_mimes['zip'] = 'application/zip';
    $existing_mimes['gz'] = 'application/x-gzip';
    return $existing_mimes;
}
add_filter('upload_mimes', 'zip_upload_mimes', 999, 1);
Run Code Online (Sandbox Code Playgroud)