Skip Navigation

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

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 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 3 replies, has 2 voices.

Last updated by Mateus Getulio 1 year ago.

Assisted by: Mateus Getulio.

Author
Posts
#2669417
screencapture.png

Hi,

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 );
#2669435

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

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.

Give it a try and tell us the results.

Best,

#2669437
screencapture-2.png

Hi Mateus,

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.

#2669641

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

I put the check a bit to late, please try the following code, it should resolve the issue:

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;

        if ( !empty($fields['_featured_image']['value']) ) {

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

Thank you.

#2669657

Hi Mateus,

perfect, it solved my problem. Thank you a lot!