I already have a script to limit field to 500 words max, but was wondering if that same script can look for a value to make sure there is at least 1 word?
If you do not want to add another hook you can use the same hook and add multiple conditions of if/else to check for multiple fields.
For example:
add_filter('cred_form_validate','func_check_validation_form_form_356',10,2);
function func_check_validation_form_form_356($error_fields, $form_data) {
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ( ($form_data['id']==356) ) {
if(!empty($fields['_featured_image']['file_data'])) {
list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['file_data']['tmp_name']);
if ($width != 500 && $height != 500) {
//set error message for featured image
$errors['_featured_image'] = 'Image size must be 500 * 500, your image dimensions are '.$width.' * '.$height;
}
}
if ( str_word_count($fields['post_content']['value']) > 500 ) {
//set error message for post_content field
$errors['post_content']='500 word count exceeded. Please, use less words for your description.';
}
}
//return result
return array($fields,$errors);
}
Sorry, one more clarification. A user can leave the featured image field blank, but if they upload an image, it needs to be in the 500x500px dimensions. How would we change the code?
if(!empty($fields['_featured_image']['file_data'])) {
list($width, $height, $type, $attr) = getimagesize($fields['_featured_image']['file_data']['tmp_name']);
if ($width != 500 && $height != 500) {
//set error message for featured image
$errors['_featured_image'] = 'Image size must be 500 * 500, your image dimensions are '.$width.' * '.$height;
}
}
I must have an overlap with another script that sets a default image. Here's what I am trying to achieve.
If user comes to CRED form and doesn't upload any image, then we set a default image for the post based on a script you all created for me.
If the user does choose to upload their own image, they should receive an validation error if that image is not 500px X 500px where they have to provide a different image in those dimensions or remove their image from the CRED form and submit the form without an image which will then attach the default image.
I believe right now the script stops them dead if they don't upload any image.
You want to allow exact 500 x 500 dimension image if uploaded or less than 500 x 500?
Can you please share problem URL where I can see the form with image field upload as well as 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 checked that you are using the following code snippet to validate the form:
- enlace oculto
I tried to upload the non 500 x 500 dimension image and it shows the correct error when image uploaded dimension is not 500 x 500. Please check the following screenshot:
- enlace oculto
When no image is uploaded, it redirect on another form with ID 208 and set the default image you set using the "cred_save_data" hook.