Skip Navigation

[Resolved] Add custom validation for featured image sizes to CRED forms

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to set required sizes for featured images uploaded with a CRED form.

Solution:
Custom validation requires custom code, such as that shown in this reply below: https://toolset.com/forums/topic/form-with-image-resolution-check/#post-586985

The validation for images occurs when the form is submitted (rather than when the image is inserted in the form and uploaded via ajax to the server).

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 7 years 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.

Our next available supporter will start replying to tickets in about 0.90 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 3 voices.

Last updated by J S 7 years ago.

Assisted by: Nigel.

Author
Posts
#586886

Hi,

I am trying to have a field with upload button which has an image check for desired dimensions. I modified the code from https://toolset.com/forums/topic/set-the-minimum-resolution-of-image-upload/ but id doesn´t work.

add_filter('cred_form_validate_488', 'cred_image_resolution_validation',10,2);
function cred_image_resolution_validation($field_data, $form_data)
{
    // Field data are field values and errors 
    list($fields,$errors)=$field_data;
   
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 99 == $form_data['id']) && (isset($_FILES['wpcf-featured-image']['tmp_name']))) {
        $check = getimagesize($_FILES["wpcf-featured-image"]["tmp_name"]);
         
        if($check !== false) {
            $width = $check[0];
            $height = $check[1];
             
            if($width !== 300 && $height !== 200) {
                $errors['the-file-field'] = 'Sorry, image has wrong dimensions.';
            }
        } else {
            $errors['the-file-field'] = 'Sorry this is not a valid image file.';   
        }
    }
 
    return array($fields,$errors);
}
#586985

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dave

It seems like the CRED system for uploading images has changed since the thread you linked to, and the code as-is will no longer work.

I rewrote the code and tested it locally and found it worked.

function validate_featured_image_size( $field_data, $form_data ){

	$form_id = array( 183 ); // add IDs of CRED forms

	$target_width = 300; // Edit
	$target_height = 200; // Edit

	if ( in_array( $form_data['id'], $form_id ) ) {

	    // Split field data into field values and errors
	    list( $fields,$errors ) = $field_data;

	    $check = getimagesize( $fields['_featured_image']['value'] );

	    if ( $check !== false ) {

	    	$width = $check[0];
	    	$height = $check[1];

	    	if ( $width != $target_width || $height != $target_height ) {
	    		$errors['_featured_image'] = "Image wrong size";
	    	}
	    }

	    $field_data = array($fields,$errors);
	}

	return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );

Note that you need to edit the first lines for the ID (or IDs) of the forms to use this on, plus the minimum required sizes for the image.

You can edit the logic and error message as required if you wanted to specify minimum dimensions, for example.

#587040

Thank you for the new code. I have edited the IDs, but nothing happened, I can upload any images.

#587077

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dave

This validation only happens when you submit the form, not when the user chooses an image and it is uploaded to the server.

Is that what you were expecting?

If you added the code and users can submit the form with incorrect images sizes then I had better take a look, as it worked ok on my local test site.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make any changes other than to temporarily add a backup plugin to take a snapshot of the site.

Can you also confirm which form and where to find it?

#587324

Hi Nigel,

I was expecting validation after uploading image. But it works after submiting the form. Thank you.

#954491

J S

Questions, relating to this:

1. Is there anyway to validate the image when the image is selected rather than waiting to validate it until the form is submitted?

2. Do the dimensions specified in the link above affect whether the image can be portrait or landscape? I don't want any restriction on the orientation of the image, but do want to be sure the images are at least a minimum size so they don't appear pixelated. Can you please elaborate on this?