library参数实际上负责在library框架中看到的内容,而不是可以上载的内容。它接受:image
,audio
,video
,file
或任何其他mime类型,例如,仅显示pdf:
library: {
type: \'application/pdf\'
},
现在,要将上载限制为文件类型,需要向上载程序添加一个参数,并使用
wp_handle_upload_prefilter
过滤器挂钩。
要添加参数,请使用:
file_frame.uploader.uploader.param( \'allowed_Type\', \'pdf\');
要过滤文件类型,请使用
add_filter(\'wp_handle_upload_prefilter\', \'Validate_upload_file_type\');
function Validate_upload_file_type($file) {
if (isset($_POST[\'allowed_Type\']) && !empty($_POST[\'allowed_Type\'])){
//this allows you to set multiple types seperated by a pipe "|"
$allowed = explode("|", $_POST[\'allowed_Type\']);
$ext = substr(strrchr($file[\'name\'],\'.\'),1);
//first check if the user uploaded the right type
if (!in_array($ext, (array)$allowed)){
$file[\'error\'] = __("Sorry, you cannot upload this file type for this field.");
return $file;
}
//check if the type is allowed at all by WordPress
foreach (get_allowed_mime_types() as $key => $value) {
if (strpos($key, $ext) || $key == $ext)
return $file;
}
$file[\'error\'] = __("Sorry, you cannot upload this file type for this field.");
}
return $file;
}