我有一个页面,我正在使用WP编辑器,允许用户在帖子中插入图像。我试图限制他们只添加图像的能力,我使用以下过滤器进行此操作:
add_filter(\'wp_handle_upload_prefilter\', \'yoursite_wp_handle_upload_prefilter\');
function yoursite_wp_handle_upload_prefilter($file) {
// This bit is for the flash uploader
if ($file[\'type\']==\'application/octet-stream\' && isset($file[\'tmp_name\'])) {
$file_size = getimagesize($file[\'tmp_name\']);
if (isset($file_size[\'error\']) && $file_size[\'error\']!=0) {
$file[\'error\'] = "Unexpected Error: {$file_size[\'error\']}";
return $file;
} else {
$file[\'type\'] = $file_size[\'mime\'];
}
}
list($category,$type) = explode(\'/\',$file[\'type\']);
if(\'image\'!=$category || !in_array($type,array(\'jpg\',\'jpeg\',\'gif\',\'png\'))) {
$file[\'error\'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
}
return $file;
}
例如,当我尝试在管理仪表板中为插件添加zip文件时,就会触发此代码,但它不允许我这样做,因为此代码已触发。我试着添加一行来检查它是否是
is_admin()
返回并不执行该代码。代码如下所示:
add_filter(\'wp_handle_upload_prefilter\', \'yoursite_wp_handle_upload_prefilter\');
function yoursite_wp_handle_upload_prefilter($file) {
// This bit is for the flash uploader
if( is_admin() ) return; //<--- Added this line
if ($file[\'type\']==\'application/octet-stream\' && isset($file[\'tmp_name\'])) {
$file_size = getimagesize($file[\'tmp_name\']);
if (isset($file_size[\'error\']) && $file_size[\'error\']!=0) {
$file[\'error\'] = "Unexpected Error: {$file_size[\'error\']}";
return $file;
} else {
$file[\'type\'] = $file_size[\'mime\'];
}
}
list($category,$type) = explode(\'/\',$file[\'type\']);
if(\'image\'!=$category || !in_array($type,array(\'jpg\',\'jpeg\',\'gif\',\'png\'))) {
$file[\'error\'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
}
return $file;
}
当我更新此内容时,在尝试上载图像时,我会收到以下错误代码:
`File is empty. Please upload something more substantial.`
如何将WP编辑器仅限于前端和管理部分的图像,而不执行此代码?是否有其他过滤器可供使用?
谢谢