[Resolved] Function for validation of featured image
This thread is resolved. Here is a description of the problem and solution.
Problem:
Validation of the featured image in the Toolset Forms was triggering an error.
Solution:
The issue was that the PHP function 'getimagesize' expects the param to be an actual object and in the error log you shared this parameter is empty.
I deducted that this happened when you try to submit the form without the featured image, then getimagesize will try to check the size of null and error out.
I added a check to see if the field _featured_image is set before checking for the size and that fixed the issue.
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.
I was using this function for validation of the featured image in the Toolset Forms. The form is not used so often, so I don't know when it actually started to fail. What could be causing the problem shown on the attached image?
function validate_featured_image_size( $field_data, $form_data ){
$form_id = array( 6352,6358 ); // add IDs of CRED forms
$target_width = 260; // Edit
$target_height = 330; // 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'] = "Obrázek nemá uvedené rozměry";
}
}
$field_data = array($fields,$errors);
}
return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
The issue seems to be that the PHP function 'getimagesize' expects the param to be an actual object and in the error log you shared this parameter is empty.
I deduct this happens when you try to submit the form without the featured image, then getimagesize will try to check the size of null and error out.
Can you please try to adjust the code a little bit to look like this:
function validate_featured_image_size( $field_data, $form_data ){
$form_id = array( 6352,6358 ); // add IDs of CRED forms
$target_width = 260; // Edit
$target_height = 330; // 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 ( empty($fields['_featured_image']['value']) === false ) {
$width = $check[0];
$height = $check[1];
if ( $width != $target_width || $height != $target_height ) {
$errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
}
}
$field_data = array($fields,$errors);
}
return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
I added a check to see if the field _featured_image is set before checking for the size.
yes, I can confirm, that it happens when I try to submit the form without the featured image. I tried the adjusted code, but there still seems to be some problem.