Restricting file uploads by type
-
Here is a hook to limit allowed uploads to images:
public function restrict_uploads() { if( is_admin() ) return; //if( current_user_can( 'unfiltered_upload' ) ) return; add_filter('wp_handle_upload_prefilter', function($file) { $allowed_mime_types = array('image/jpeg','image/gif','image/png' ,'image/bmp','image/tiff'); if( !in_array($file['type'], $allowed_mime_types) ) $file['error'] = 'Only image files of the follow types are allowed for upload: .jpg, .jpeg, .jpe, .gif, .png, .bmp, .tif, .tiff'; return $file; }); } add_action('bbp_edit_reply', 'restrict_uploads', 9); add_action('bbp_edit_topic', 'restrict_uploads', 9); add_action('bbp_new_reply', 'restrict_uploads', 9); add_action('bbp_new_topic', 'restrict_uploads', 9);Add that to your functions.php.
There is a bug in the plugin you will have to fix as well in /code/attachments/front.php. Staring around line 118 change this:
if (!is_wp_error($upload)) { $errors->add('wp_upload', $upload->errors['wp_upload_error'][0], $file_name); } else { $errors->add('wp_upload', $upload->errors['wp_upload_error'][0], $file_name); }to this:
if (is_wp_error($upload)) { $errors->add('wp_upload', $upload->errors['wp_upload_error'][0], $file_name); } elseif (!empty($upload['error'])) { $errors->add('wp_upload', $upload['error'], $file_name); } else { $uploads[] = $upload; }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘Restricting file uploads by type’ is closed to new replies.