Created
January 18, 2024 14:05
-
-
Save taricco/60d3d516b9f3d6e8802b30d84f4aca32 to your computer and use it in GitHub Desktop.
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
Replace the **formid** and **fieldid** | |
/*** Gravity Forms - Restrict the file upload size for Guest Blog form | |
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/ | |
add_filter( 'gform_validation', 'validate_file_size_form_name' ); | |
function validate_file_size_form_name( $validation_result ) { | |
$form = $validation_result['form']; | |
// Specify your target form ID | |
$target_form_id = **formid**; | |
// Check if the current form is the one you want to target | |
if ( $form['id'] != $target_form_id ) { | |
return $validation_result; | |
} | |
foreach ( $form['fields'] as &$field ) { | |
// Check if the field is a File Upload field and has the specific field ID | |
if ( $field->type != 'fileupload' || $field->id != **fieldid** ) { | |
continue; | |
} | |
// Construct the field ID for $_FILES | |
$field_id = 'input_' . $field->id; | |
if ( isset( $_FILES[ $field_id ] ) && !empty( $_FILES[ $field_id ]['name'] ) ) { | |
$file_size = $_FILES[ $field_id ]['size']; | |
// Set your size limit (300KB in this case) | |
if ( $file_size > 300 * 1024 ) { | |
$validation_result['is_valid'] = false; | |
$field->failed_validation = true; | |
$field->validation_message = 'File size must be less than 300KB.'; | |
} | |
} | |
} | |
$validation_result['form'] = $form; | |
return $validation_result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment