Hello. Thank you for contacting the Toolset support.
Can you please share the problem URL where you added the form as well as access details so that I can check what's going wrong
there.
*** 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 adjusted the code to "Custom Code" section with "toolset-restrict-images" as given under:
=> hidden link
function md_validate_image_size_resolution( $field_data, $form_data ){
$form_id = array( 266, 704 ); // add IDs of CRED forms
$target_size = 500000; // adjust the allowed size
if ( in_array( $form_data['id'], $form_id ) ) {
// Split field data into field values and errors
list( $fields,$errors ) = $field_data;
//image in bytes allowed maximum dimension
$target_allowed_size = 500000;
//featured image allowed dimensions
$featured_image_min_width = 1920;
$featured_image_max_width = 2200;
$featured_image_min_height = 600;
$featured_image_max_height = 1080;
//gallery image allowed dimensions
$gallery_image_min_width = 960;
$gallery_image_max_width = 1600;
$gallery_image_min_height = 540;
$gallery_image_max_height = 900;
$fimg = $fields['_featured_image']['file_data']['tmp_name'] ;
$check = getimagesize($fimg);
if($fields['_featured_image']['file_data']['size'] > $target_allowed_size || $width < $featured_image_min_width || $width > $featured_image_max_width || $height < $featured_image_min_height || $height > $featured_image_max_height)
{
$fmsgdim='';$fmsgminw ='';$fmsgmaxw ='';$fmsgminh ='';$fmsgmaxh = '';
if($fields['_featured_image']['file_data']['size'] > $target_allowed_size){
$fmsgdim = "Dimensiunea fișierului este de ". $fields['_featured_image']['file_data']['size'] / 1000 ." kB, mai mare decât maximul acceptat de " . $target_allowed_size/1000 . ' kB; <br/>';
}
if($check[0] < $featured_image_min_width )
{
$fmsgminw = "Lățime prea mică (". check[0] ."). Minum acceptat: " . $featured_image_min_width .'px; <br/>';
echo $fimg;
}
if($check[0] > $featured_image_max_width )
{
$fmsgmaxw = "Lățime prea mare. Maxim acceptat: " . $featured_image_max_width .'px; <br/>';
}
if($check[1] < $featured_image_min_height )
{
$fmsgminh = "Înălțime prea mică. Minum acceptat: " . $featured_image_min_height .'px; <br/>';
}
if($check[1] > $featured_image_max_height )
{
$fmsgmaxh = "Înălțime prea mare. Maxim acceptat: " . $featured_image_max_height .'px; <br/>';
}
$errors['_featured_image'] = $fmsgdim . $fmsgminw . $fmsgmaxw . $fmsgminh . $fmsgmaxh;
}
if (isset($_POST['wpcf-imagine-galerie-foto-1']))
{
if($fields['wpcf-imagine-galerie-foto-1']['file_data']['size'] > $target_allowed_size)
{
$errors['wpcf-imagine-galerie-foto-1'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
}
}
if (isset($_POST['wpcf-imagine-galerie-foto-2']))
{
if($fields['wpcf-imagine-galerie-foto-2']['file_data']['size'] > $target_allowed_size)
{
$errors['wpcf-imagine-galerie-foto-2'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
}
}
if (isset($_POST['wpcf-imagine-galerie-foto-1']))
{
if($fields['wpcf-imagine-galerie-foto-3']['file_data']['size'] > $target_allowed_size)
{
$errors['wpcf-imagine-galerie-foto-3'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
}
}
if (isset($_POST['wpcf-imagine-galerie-foto-4']))
{
if($fields['wpcf-imagine-galerie-foto-4']['file_data']['size'] > $target_allowed_size)
{
$errors['wpcf-imagine-galerie-foto-4'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
}
}
if (isset($_POST['wpcf-imagine-galerie-foto-5']))
{
if($fields['wpcf-imagine-galerie-foto-5']['file_data']['size'] > $target_allowed_size)
{
$errors['wpcf-imagine-galerie-foto-5'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
}
}
if (isset($_POST['wpcf-imagine-galerie-foto-6']))
{
if($fields['wpcf-imagine-galerie-foto-6']['file_data']['size'] > $target_allowed_size)
{
$errors['wpcf-imagine-galerie-foto-6'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
}
}
}
return array($fields,$errors);
}
add_action( 'cred_form_validate', 'md_validate_image_size_resolution', 10, 2 );
There were the following issues that I fixed:
- first, I've activated the snippet as it was inactive
- then, I've changed the hook name from cred_save_data to cred_form_validate.
- then, I've changed/adjusted the following line of code from:
Just one last question: would it be possible (if not too difficult to implement) to clear the "to-be-uploaded" image from there if the submission fails? Because if a user tries the second time to upload the form, replacing the image in discussion, the image still shows there but the new error message is different?