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