Hey
We have a form for submitting a new "Party" (CTP for our site). In it, there are images fields (The " Allow multiple-instances of this field" is checked - so user can add several images)
My question - how can I limit the users from uploading a very large image? i.e- limit the image upload size to less than 1MB
Hi, you can use the Forms API and a custom code snippet to validate each uploaded image. Add this code to your child theme's functions.php file or create a new snippet in Toolset > Settings > Custom code:
add_filter('cred_form_ajax_upload_validate','rep_img_size_validation',10,2);
function rep_img_size_validation($error_fields, $form_data)
{
$forms = array( 123, 456 );
$size = 1024000;
$field_slug = 'your-repeating-image-slug';
$error_msg = 'Maximum size is 1MB, please try again.';
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if (in_array( $form_data['id'], $forms ) && isset($fields['wpcf-' . $field_slug]['field_data']['size']))
{
//check if this field instance img is bigger than $size (bytes)
$instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
if ( $instance_size > $size )
{
//display error message for this field instance
$errors['wpcf-' . $field_slug] = $error_msg;
}
}
//return result
return array($fields,$errors);
}
Replace 123, 456 with a comma-separated list of Form IDs where you want to validate this image. Usually you'll include both the create post and edit post Form IDs here, but any number of IDs is okay. I have set the size to 1024000 ( number of bytes in 1MB ), but you can modify that if you'd like. Change the field slug to be exactly what you see in wp-admin. Modify the error message as needed, and you should be all set.
I made a small adjustment to this code, so be sure to copy the recent version from the forum.