Skip to content

Instantly share code, notes, and snippets.

@mandiwise
Last active January 16, 2022 14:12
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mandiwise/f66f82d15220bc3fdc74 to your computer and use it in GitHub Desktop.
Save mandiwise/f66f82d15220bc3fdc74 to your computer and use it in GitHub Desktop.
Set a maximum upload size for a Gravity Forms image field
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