I've been using this hook to validate image uploads:-
if ( ( !empty( $fields['wpcf-image-1']['value'] )) && ( isset($_FILES['wpcf-image-1']['type'] )) && ( isset($_FILES['wpcf-image-1']['size'] )) ) {
$file_type_uploaded=$_FILES['wpcf-image-1']['type'];
$file_size_uploaded=$_FILES['wpcf-image-1']['size'];
if ( ( $file_type_uploaded != 'image/jpeg' ) || ( $file_size_uploaded > 10240 ) ) {
$errors['image-1']='Please check your image is a JPG/JPEG and smaller than 10KB in size';
}
}
but I've noticed the following:-
1. when one or more images are uploaded when the post is first created, the snippet works
2. when the post is subsequently updated using the edit version of the form, the snippet returns the error message for previously uploaded images and any new/replacement ones even if they are of the right type and size.
The setup:-
1. image fields are not repeating fields
2. the media library is not enabled for the frontend
3. I'm using the cred_form_validate hook not AJAX (this is disabled via the cred filter)
Any ideas please?
Hi Julie,
Thank you for contacting us and I'd be happy to assist.
The code snippet that you shared won't work for the already uploaded images since it checks for "existence" in the "$_FILES" and not the "emptiness".
When editing a post which already includes an image in the field and no new image is uploaded, the condition in the snippet becomes true even when it shouldn't, due to "isset". In this case, the "$_FILES['wpcf-image-1']['type']" and "$_FILES['wpcf-image-1']['size']" variables exists, but with empty values.
For this reason, it would be best to replace:
if ( ( !empty( $fields['wpcf-image-1']['value'] )) && ( isset($_FILES['wpcf-image-1']['type'] )) && ( isset($_FILES['wpcf-image-1']['size'] )) ) {
With:
if ( ( !empty( $fields['wpcf-image-1']['value'] )) && ( !empty($_FILES['wpcf-image-1']['type'] )) && ( !empty($_FILES['wpcf-image-1']['size'] )) )
I hope this helps.
regards,
Waqar
Hi Waqar
Ah yes that subtle but important difference between 'existence' and 'emptiness'; I've changed my snippet and re-tested successfully.
Many thanks for your help; much appreciated