Home›Types Community Support›[Resolved] in cred form I want photos to be accepted only if they are of a particular size.
[Resolved] in cred form I want photos to be accepted only if they are of a particular size.
The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.
I am trying to:
Made a cred form. There is no option where I can specify the dimensions of photo before a photo can be submitted.
Link to a page where the issue can be seen: hidden link
I expected to see:
there should be some place where photos of only a particular size can be submitted.
Instead, I got:
a simple media upload button
You should be able to achieve this by using the hook below.
add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']==2991)
{
//check if featured image exists o
if (isset($fields['_featured_image']))
{
list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['field_data']['tmp_name']);
if ($width != 1220 && $height != 360) {
//set error message for featured image
$errors['_featured_image'] = 'Image size must be 1220 * 360, your image dimensions are '.$width.' * '.$height;
}
}
}
//return result
return array($fields,$errors);
}
What you need to do is to change _featured_image to the slug of your image field and using the wpcf- prefix in front of the slug.
Next you need to change the $width != 200 and the $height != 360 to the appropriate dimensions that you want.
The issue was that you needed to have change the id in the code for the form its checking to 12 and the field that it should've applied to was the _featured_image field.
Please check on your end and it should now be working.
Hi. I edited something and its not working. Now the form accepts any size of image. Post field name is imagez and form number is 12. My code is this -
add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']==5)
{
//check if featured image exists o
if (isset($fields['_featured_image']))
{
list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['field_data']['tmp_name']);
if ($width != 200 && $height != 280) {
//set error message for featured image
$errors['Image-size-error'] = 'Image size must be 280 (height) * 200 (width), your image dimensions are '.$height.' * '.$width;
}
}
}
//return result
return array($fields,$errors);
}
add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']==12)
{
//check if featured image exists o
if (isset($fields['wpcf-imagez']))
{
list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['field_data']['tmp_name']);
if ($width != 200 && $height != 280) {
//set error message for featured image
$errors['Image-size-error'] = 'Image size must be 280 (height) * 200 (width), your image dimensions are '.$height.' * '.$width;
}
}
}
//return result
return array($fields,$errors);
}