• 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;
                            }

    https://ww.wp.xz.cn/plugins/gd-bbpress-attachments/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Milan Petrovic

    (@gdragon)

    This free plugin doesn’t have direct way to control allowed file types, but Pro plugin GD bbPress Toolbox Pro has this option with additional controls to add new file types: http://www.gdbbpbox.com/features/attachments/.

    Regards,
    Milan

    Thread Starter chaoix

    (@chaoix)

    Milan,

    Thanks for the information on the pro plugin. I didn’t see this feature when I looked at pro before writing the code above.

    Did my fix to the upload error handling make its way into the code?

    Matthew

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Restricting file uploads by type’ is closed to new replies.