admin管理员组文章数量:1023773
Somebody knows some trick in Add new Post to:
Disable the upload of audio, video and other filetypes.
Only accept the upload of an image (jpg, png, gif).
Limit the upload of each Post to only One image (no more than one).
Thanks in advance.
Somebody knows some trick in Add new Post to:
Disable the upload of audio, video and other filetypes.
Only accept the upload of an image (jpg, png, gif).
Limit the upload of each Post to only One image (no more than one).
Thanks in advance.
Share Improve this question asked Jan 30, 2011 at 5:00 José Pablo Orozco MarínJosé Pablo Orozco Marín 1,2012 gold badges18 silver badges36 bronze badges 1- 1 You can also modify the Flash uploader to allow only one file. This works together with the solution Mike gave. – Jan Fabry Commented Apr 26, 2011 at 14:07
1 Answer
Reset to default 27I was about to give up thinking that it wasn't possible or at least easy and then I stumbled onto the wp_handle_upload_prefilter
filter which gives you exactly what you asked for! Here's the code:
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.";
} else if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
$file['error'] = "Sorry, you cannot upload more than one (1) image.";
}
return $file;
}
And here are some screenshots showing how it looks in action:
Somebody knows some trick in Add new Post to:
Disable the upload of audio, video and other filetypes.
Only accept the upload of an image (jpg, png, gif).
Limit the upload of each Post to only One image (no more than one).
Thanks in advance.
Somebody knows some trick in Add new Post to:
Disable the upload of audio, video and other filetypes.
Only accept the upload of an image (jpg, png, gif).
Limit the upload of each Post to only One image (no more than one).
Thanks in advance.
Share Improve this question asked Jan 30, 2011 at 5:00 José Pablo Orozco MarínJosé Pablo Orozco Marín 1,2012 gold badges18 silver badges36 bronze badges 1- 1 You can also modify the Flash uploader to allow only one file. This works together with the solution Mike gave. – Jan Fabry Commented Apr 26, 2011 at 14:07
1 Answer
Reset to default 27I was about to give up thinking that it wasn't possible or at least easy and then I stumbled onto the wp_handle_upload_prefilter
filter which gives you exactly what you asked for! Here's the code:
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.";
} else if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
$file['error'] = "Sorry, you cannot upload more than one (1) image.";
}
return $file;
}
And here are some screenshots showing how it looks in action:
本文标签: Limit image upload to one and disable audiovideo and other document file types to upload
版权声明:本文标题:Limit image upload to one and disable audio, video and other document file types to upload 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745522482a2154376.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论