Skip Navigation

[Resolved] Unable to restrict file size upload for wordpress featured image on CRED form

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by himanshuS 3 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1971077

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);
}
#1971777

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

#1972335

You are the man, Waqar! My issue is resolved now. Thank you!