Skip Navigation

[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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by simranjeetS-2 6 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#619914
Screenshot 2018-02-27 14.15.30.png

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 already have my login details

#620278

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for contacting our support forum.

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.

Please let me know if this helps.
Thanks,
Shane

#621319

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Was this solution able to resolve the issue for you ?

Please let me know.
Thanks,
Shane

#621524

Hi. The solution is not working. Should I give you admin? I have changed admin info.

#621543

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Yes please provide me with the admin information as well as the page where this form is and the field that you want the code to apply to.

Thanks,
Shane

#621564

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simran,

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.

Thanks,
Shane

#621599

Thanks a lot. This is working. After checking, I made a few changes and the form still works.

#622227

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

Please correct it

#622237

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simran,

Please try 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']==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);
}
#622512

Thanks for this. Everything is working except error line-

Image-size-error: Image size must be 280 (height) * 200 (width), your image dimensions are *

It doesnt give size

#624341
Screenshot 2018-03-12 22.32.28.png

wpcf-imagez is the key. Everything is working. Thanks a lot

The forum ‘Types Community Support’ is closed to new topics and replies.