函数文档

check_upload_mimes()

💡 云策文档标注

概述

check_upload_mimes() 函数用于根据 WordPress 多站点超级管理员设置的文件类型列表,过滤传入的 MIME 类型数组。它基于 get_allowed_mime_types() 定义的允许上传文件类型,确保仅返回允许的类型。

关键要点

  • 函数检查 MIME 类型数组是否在允许列表中,常用于多站点环境下的上传文件类型控制。
  • 参数 $mimes 为必需数组,包含待检查的 MIME 类型映射(如扩展名模式 => MIME 类型)。
  • 返回过滤后的数组,仅包含允许的 MIME 类型。
  • 内部使用 get_site_option('upload_filetypes') 获取站点允许的文件扩展名列表。

代码示例

$mimes = array(
    'jpg|jpeg|jpe' => 'image/jpeg',
    'php'          => 'application/x-php', // 此类型不在白名单中!
);
$mimes = check_upload_mimes( $mimes );
print_r( $mimes );

📄 原文内容

Checks an array of MIME types against a list of allowed types.

Description

WordPress ships with a set of allowed upload filetypes, which is defined in wp-includes/functions.php in get_allowed_mime_types() . This function is used to filter that list against the filetypes allowed provided by Multisite Super Admins at wp-admin/network/settings.php.

Parameters

$mimesarrayrequired

Return

array

Source

function check_upload_mimes( $mimes ) {
	$site_exts  = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
	$site_mimes = array();
	foreach ( $site_exts as $ext ) {
		foreach ( $mimes as $ext_pattern => $mime ) {
			if ( '' !== $ext && str_contains( $ext_pattern, $ext ) ) {
				$site_mimes[ $ext_pattern ] = $mime;
			}
		}
	}
	return $site_mimes;
}

Changelog

Version Description
MU (3.0.0) Introduced.

User Contributed Notes