Set a maximum upload size for a Gravity Forms image field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function limit_file_upload_size( $validation_result ) { | |
$form = $validation_result['form']; | |
foreach( $form['fields'] as &$field ){ | |
// NOTE: Add a custom CSS class to your image upload field and grab onto it here... | |
if( strpos( $field['cssClass'], 'choose-file' ) === false ) | |
continue; | |
$is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() ); | |
if( $is_hidden ) | |
continue; | |
$input_name = 'input_' . $field['id']; | |
$temp_filename = RGFormsModel::get_temp_filename($form['id'], $input_name); | |
$temp_location = RGFormsModel::get_upload_path($form['id']) . '/tmp/' . $temp_filename['temp_filename']; | |
if ( isset( $_FILES[$input_name] ) && !empty( $_FILES[$input_name] ) ) | |
$filesize = $_FILES[$input_name]['size']; | |
elseif ( file_exists( $temp_location ) ) | |
$filesize = filesize( $temp_location ); | |
else | |
continue; | |
// NOTE: 75 = the max. KB size ... | |
if ( $filesize > 75 * 1024 ) { | |
$validation_result['is_valid'] = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = 'Maximum logo file size cannot exceed 75 KB.'; | |
unset( RGFormsModel::$uploaded_files[$form['id']][$input_name], $_FILES[$input_name] ); | |
continue; | |
} | |
} | |
$validation_result['form'] = $form; | |
return $validation_result; | |
} | |
add_filter('gform_validation_[your form ID]', 'limit_file_upload_size'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment