Skip Navigation

[Resolved] CRED form featured image size limitation not working

This support ticket is created 4 years, 10 months 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.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by alinaB 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1450105

I am trying to:
I am trying to use the code:

add_filter('cred_form_ajax_upload_validate','my_validation',8,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']==266)
    {
        //check if featured image exceeds some size
        if ($fields['_featured_image']['field_data']['size'] > 10)
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Wrong size image';
        }
    }
    //return result
    return array($fields,$errors);
}

within a CRED form having the id 266 in order to limit the featured image file size on upload.
Nothing happens -- I am able to upload any filesize.

In the same snippet, I am executing also some other stuff

add_action('cred_save_data', 'copy_location_to_loc_field',10,2);
add_action('cred_save_data','func_custom_post_content',9,2);

which functions very well.

I was trying to use as references:
1. https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
2. https://toolset.com/forums/topic/how-to-limit-the-file-upload-size-limit-from-the-cred-form/

with variations. None of them provided the expected result.

The form submit has the option "Submit this form without reloading the page (use AJAX)" checked.

I need guidance, please. Thank you in advance.

#1450501

OK, so I have seen in this forum that the ajax upload does not work anymore in CRED. I have tried with

add_filter('cred_form_validate','my_validation',8,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']==266)
    {
        //check if featured image exceeds some size
        if ($_Post['_featured_image']['field_data']['size'] > 10)
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Wrong size image';
        }
    }
    //return result
    return array($fields,$errors);
}

but still no result -- any image size is accepted and uploaded.

Any ideas how I can fix this?

#1450503

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share the problem URL where I can see the form?
- are you using the form setting 'Use the WordPress Media Library manager for image, video, audio, or file fields' to upload the image?
- the form should be available to logged-in users or guest users as well?

#1450569

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It looks like we crossposted. Can you please share the details I asked and I will be happy to assist you further.
Or
Maybe you should share the problem URL of the page where you added the form and validate the featured image.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1450585

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've made your last reply as private so only you and me can see it

I need wp-admin access details. Can you please send me temporary admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1450869

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following code to "Custom Code" section offered by Toolset and removed it from the snippet where you added it originally.
=> hidden link

//This tries to limit the featured image size in kB
function func_validate_featured_image_size( $field_data, $form_data ){
 
    $form_id = array( 266 ); // add IDs of CRED forms
   
 if ( in_array( $form_data['id'], $form_id ) ) {
 
        // Split field data into field values and errors
        list( $fields,$errors ) = $field_data;
 
        $target_allowed_size = 100000;  // ajust the target allowed size 

        if($fields['_featured_image']['file_data']['size'] > $target_allowed_size){
           $errors['_featured_image'] = "Image wrong size";
        }
 
    }
 
   return array($fields,$errors);
}
add_action( 'cred_form_validate', 'func_validate_featured_image_size', 10, 2 );

Where:
- Adjust the $target_allowed_size as per your requirement and error message.

More info;
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

#1451095

My issue is resolved now. Thank you!