Allowing WebP uploads?

时间:2018-12-17 作者:Best Dev Tutorials

我已经尝试了这两种方法,但仍然收到错误消息:“抱歉,出于安全原因,不允许使用此文件类型。”

// add support for webp mime types
function webp_upload_mimes( $existing_mimes ) {
    // add webp to the list of mime types
    $existing_mimes[\'webp\'] = \'image/webp\';

    // return the array back to the function with our added mime type
    return $existing_mimes;
}

add_filter( \'mime_types\', \'webp_upload_mimes\' );


function my_custom_upload_mimes($mimes = array()) {

   // add webp to the list of mime types
    $existing_mimes[\'webp\'] = \'image/webp\';

    return $mimes;
}

add_action(\'upload_mimes\', \'my_custom_upload_mimes\');
有什么想法吗?

3 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

有必要使用wp_check_filetype_and_ext 过滤器设置webp文件的mime类型和扩展名,以及使用upload_mimes 筛选器将mime类型添加到可上载mime的列表中。

/**
 * Sets the extension and mime type for .webp files.
 *
 * @param array  $wp_check_filetype_and_ext File data array containing \'ext\', \'type\', and
 *                                          \'proper_filename\' keys.
 * @param string $file                      Full path to the file.
 * @param string $filename                  The name of the file (may differ from $file due to
 *                                          $file being in a tmp directory).
 * @param array  $mimes                     Key is the file extension with value as the mime type.
 */
add_filter( \'wp_check_filetype_and_ext\', \'wpse_file_and_ext_webp\', 10, 4 );
function wpse_file_and_ext_webp( $types, $file, $filename, $mimes ) {
    if ( false !== strpos( $filename, \'.webp\' ) ) {
        $types[\'ext\'] = \'webp\';
        $types[\'type\'] = \'image/webp\';
    }

    return $types;
}

/**
 * Adds webp filetype to allowed mimes
 * 
 * @see https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_mimes
 * 
 * @param array $mimes Mime types keyed by the file extension regex corresponding to
 *                     those types. \'swf\' and \'exe\' removed from full list. \'htm|html\' also
 *                     removed depending on \'$user\' capabilities.
 *
 * @return array
 */
add_filter( \'upload_mimes\', \'wpse_mime_types_webp\' );
function wpse_mime_types_webp( $mimes ) {
    $mimes[\'webp\'] = \'image/webp\';

  return $mimes;
}
我在WP v5.0.1上对此进行了测试,并能够在添加此代码后上载webp文件。

SO网友:Johansson

有时上载受到主机的限制。尝试定义ALLOW_UNFILTERED_UPLOADS 允许上载每种文件类型的常量:

define( \'ALLOW_UNFILTERED_UPLOADS\', true );
在您的wp-config.php 暂时归档,以便测试。然后重新上传你的文件,如果仍然无法正常工作,很可能是主机阻止了该文件类型的上传。在完成尝试后,请确保尽快删除常量。

此外,您可以使用get_allowed_mime_types() 函数检查允许的上载模拟。

SO网友:wp-overwatch.com

我可以通过创建以下插件来实现它。(您只需将此代码添加到/wp-content/plugins/ 创建插件的目录。确保在创建插件后激活它。)

<?php
/**
 * Plugin Name: WPSE Allow Webp Uploads
 */

function webp_file_and_ext( $mime, $file, $filename, $mimes ) {

    $wp_filetype = wp_check_filetype( $filename, $mimes );
    if ( in_array( $wp_filetype[\'ext\'], [ \'webp\' ] ) ) {
        $mime[\'ext\']  = true;
        $mime[\'type\'] = true;
    }

    return $mime;
}
add_filter( \'wp_check_filetype_and_ext\', \'webp_file_and_ext\', 10, 4 );

function add_webp_mime_type( $mimes ) {
    $mimes[\'webp\'] = \'image/webp\';
    return $mimes;
}
add_filter( \'upload_mimes\', \'add_webp_mime_type\' );

相关推荐

ADD_THEME_SUPPORT(‘CUSTOM-HEADER-UPLOADS’);功能有什么作用?

同时向functions.php 文件中,我发现了以下内容:add_theme_support(\'custom-header-uploads\');这到底是做什么的?我删除了条目functions.php 但对网站没有什么不同。我最初认为这与上传标题图像的能力有关,但这取决于add_theme_support(\'custom-header\');在执行搜索时,我无法在Codex中看到任何有意义的信息。这是某种贬值的特征还是我忽略了什么?