Home › Toolset Professional Support › [Resolved] File size restriction for featured image – filter not working
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 |
---|---|---|---|---|---|---|
- | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9: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/Karachi (GMT+05:00)
Tagged: Content-submission forms, CRED API, Toolset Forms
Related documentation:
This topic contains 11 replies, has 2 voices.
Last updated by mikes-33 3 years, 5 months ago.
Assisted by: Waqar.
I have this:
add_filter('cred_form_ajax_upload_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //validate if specific form $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; if ( ($form_data['id']==582) ) { if(isset($fields['_featured_image'])) { //Retrieve file type $file_type_uploaded=$fields['_featured_image']['field_data']['type']; //Retrieve file size $file_size_uploaded=$fields['_featured_image']['field_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5120000) || ($file_type_uploaded != 'image/jpg') ) { //set error message for featured image $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; } } } //return result return array($fields,$errors); }
My form ID is 582... and field '_featured_image'. I've tried 'cred_form_ajax_upload_validate' and 'cred_form_validate'.
It simply doesn't fire. You can see I've put the error trigger so it creates an error no matter what - just to test... it still doesn't fire at all... I can upload whatever I want and it ignores the validation completely.
Hi,
Thank you for contacting us and I'd be happy to assist.
The filter 'cred_form_ajax_upload_validate' specifically targets the event when a new image is uploaded into the WordPress media library through Toolset Forms.
For this filter to trigger, the following conditions need to be met:
1. In the form's settings, the 'Use the WordPress Media Library manager for image, video, audio, or file fields' option needs to be checked.
AND
2. The new image should be uploaded as it won't fire if an existing image from the media library is selected.
AND
3. The visitor who is submitting the form, needs to be logged in as a user. For guests (non-logged-in), this filter won't be triggered because the guests can't upload images into the media library and a standard image upload field is shown when they are submitting the form.
I've tested the function that you've shared and it does work as expected when the above conditions are met.
I hope this helps and let me know how it goes.
regards,
Waqar
Thanks for the information. My question then is...
I need to allow not-logged-in users to submit posts, including featured image.
This works - the post is added as a draft. The image appears in Media Library. (but validation does nothing as above).
I don't want guests having any access to anything in the media library - they can add an image, but not see existing media.
How then do I validate the images they are adding? What filter will work?
or
Does 'Use WP library' mean guests can see media? Or only add files through it?
Thanks
Use WP Library does not show images to not-logged-in users... I just checked.
So my only question is - how do i validate images uploaded by not-logged-in users?
Thanks for writing back.
To cover the validation for the file uploaded by the guest (non-logged-in) users, you can use the 'cred_form_validate' hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
You'll find the example snippet in this reply from Minesh:
https://toolset.com/forums/topic/cred-form-featured-image-size-limitation-not-working/#post-1450869
Perhaps I wasn't clear. As per my original ticket... I've tried 'cred_form_ajax_upload_validate' and 'cred_form_validate'.
Neither do anything.
I've set this up now:
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //validate if specific form $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; if ( ($form_data['id']==582) ) { if(isset($fields['_featured_image'])) { //Retrieve file type $file_type_uploaded=$fields['_featured_image']['field_data']['type']; //Retrieve file size $file_size_uploaded=$fields['_featured_image']['field_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5) ) { //set error message for featured image $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; } } } //return result return array($fields,$errors); }
I then upload a PNG of 200k and still nothing happens. It's clearly not even being triggered, as I set the error OUTSIDE the IF statement as well as inside it. I've set it to 'allow WP media manager'. Nothing happens.
Perhaps I wasn't clear. As per my original ticket... I've tried 'cred_form_ajax_upload_validate' and 'cred_form_validate'.
Neither do anything.
I've set this up now:
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //validate if specific form $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; if ( ($form_data['id']==582) ) { if(isset($fields['_featured_image'])) { //Retrieve file type $file_type_uploaded=$fields['_featured_image']['field_data']['type']; //Retrieve file size $file_size_uploaded=$fields['_featured_image']['field_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5) ) { //set error message for featured image $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; } } } //return result return array($fields,$errors); }
I then upload a PNG of 200k and still nothing happens. It's clearly not even being triggered, as I set the error OUTSIDE the IF statement as well as inside it. I've set it to 'allow WP media manager'. Nothing happens.
Perhaps I wasn't clear. As per my original ticket... I've tried 'cred_form_ajax_upload_validate' and 'cred_form_validate'.
Neither do anything.
I've set this up now:
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //validate if specific form $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; if ( ($form_data['id']==582) ) { if(isset($fields['_featured_image'])) { //Retrieve file type $file_type_uploaded=$fields['_featured_image']['field_data']['type']; //Retrieve file size $file_size_uploaded=$fields['_featured_image']['field_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5) ) { //set error message for featured image $errors['Image Upload'] = 'Unrecognised. Please upload a JPG under 5Mb.'; } } } //return result return array($fields,$errors); }
I then upload a PNG of 200k and still nothing happens. It's clearly not even being triggered, as I set the error OUTSIDE the IF statement as well as inside it. I've set it to 'allow WP media manager'. Nothing happens.
Thanks for writing back.
During troubleshooting, I was able to make your function work, after making these changes:
1. At line# 14 and 17, you're using ['field_data'], which should be '[file_data]'.
2. In your function, you're adding errors to $errors['Image Upload'] but the field is '_featured_image' so it should be $errors['_featured_image'].
To summarize what we've discussed:
1. The function attached to 'cred_form_ajax_upload_validate' hook would trigger when a logged-in user is uploading a new file, and the form is set to enable the uploads through the media library.
2. The function attached to the 'cred_form_validate' hook is triggered when the form is submitted, and
a). the visitor is not logged in
or
b). the visitor is logged in but the option to use the media library for the uploads is disabled.
Hi. Thanks... that's strange, because every ticket I found uses FIELD_DATA - but yes, it works with FILE_DATA.
So we're nearly there. My code is now as follows (I need to validate two files...)
It validates the audio fine - and if there's no audio it ignores it.
It validates the image when present - BUT IT REJECTS THE POST IF THERE'S NO IMAGE.
The image is not required (I set a default if it's not there later on). Can you tell me why it rejects even when the image is blank. Thanks.
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_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']==582) ) { if(isset($_FILES['_featured_image']['file_data']['type'])) { //Retrieve file type $file_type_uploaded=$fields['_featured_image']['file_data']['type']; //Retrieve file size $file_size_uploaded=$fields['_featured_image']['file_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5120000) || ($file_type_uploaded != 'image/jpeg') ) { //set error message for featured image $errors['_featured_image'] = 'Please upload a JPG under 5Mb.'; } } if(isset($fields['audioup']['file_data']['type'])) { //Retrieve file type $file_type_uploaded=$fields['audioup']['file_data']['type']; //Retrieve file size $file_size_uploaded=$fields['audioup']['file_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5120000) || ($file_type_uploaded != 'audio/mpeg') ) { //set error message for featured image $errors['audioup'] = 'Type: ' . $file_type_uploaded . ' -Size: ' . $file_size_uploaded; } } } //return result return array($fields,$errors); }
Thanks for the update and glad that we're making progress.
I see 2 issues in your latest code:
1. At line# 10, the array '$fields' should've been used instead of '$_FILES'.
2. And at the same line, you're checking whether or not a featured image's type is set. In case of no featured image is added, the type in the array is set but with an empty value. As a result, the if statement for the featured image becomes true, but as the image's type is not 'image/jpeg', the error for the featured image field is registered.
The correct usage, at line# 10 would be to replace the 'isset' check with a 'not-empty' check:
if(!empty($fields['_featured_image']['file_data']['type'])) {
Yes, I tried $_FILES as part of my experimenting, but no it doesn't work! Your fix does so it's all working now, thank you.
In case anyone finds this thread, here is the working code.
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_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']==582) ) { if(!empty($fields['_featured_image']['file_data']['type'])) { //Retrieve file type $file_type_uploaded=$fields['_featured_image']['file_data']['type']; //Retrieve file size $file_size_uploaded=$fields['_featured_image']['file_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5120000) || ($file_type_uploaded != 'image/jpeg') ) { //set error message for featured image $errors['_featured_image'] = 'Please upload a JPG under 5Mb.'; } } if(isset($fields['audioup']['file_data']['type'])) { //Retrieve file type $file_type_uploaded=$fields['audioup']['file_data']['type']; //Retrieve file size $file_size_uploaded=$fields['audioup']['file_data']['size']; //check if featured image exists if ( ($file_size_uploaded > 5120000) || ($file_type_uploaded != 'audio/mpeg') ) { //set error message for featured image $errors['audioup'] = 'Type: ' . $file_type_uploaded . ' -Size: ' . $file_size_uploaded; } } } //return result return array($fields,$errors); }