I want to restrict the image file size for featured image when using CRED form. I looked at some of the code already shared in forums but the code does not seem to work. What could I be missing?
Code
add_filter('cred_form_validate','project_image_size_validation',10,2);
function project_image_size_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ($form_data['id']== 5371 || $form_data['id']== 6440 || $form_data['id']== 5369 || $form_data['id']== 6439 || $form_data['id']== 11977)
{
//get the latest image size
$imagedata_array = array();
$imagedata_array[] = $fields['_featured_image']['field_data']['size'];
$size = end($imagedata_array);
if ($size > 1000000)
{
$errors['_featured_image'] = 'the image size should not be more than 1MB';
}
}
return array($fields,$errors);
}
Hi,
I've run some tests and was able to make this validation function work, after slight adjustments:
add_filter('cred_form_validate','project_image_size_validation',10,2);
function project_image_size_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ($form_data['id']== 5371 || $form_data['id']== 6440 || $form_data['id']== 5369 || $form_data['id']== 6439 || $form_data['id']== 11977)
{
$featured_image_size = $fields['_featured_image']['file_data']['size'];
if ($featured_image_size > 1000000)
{
$errors['_featured_image'] = 'the image size should not be more than 1MB';
}
}
return array($fields,$errors);
}
regards,
Waqar
You are the man, Waqar! My issue is resolved now. Thank you!