Tell us what you are trying to do?
I would like to set my site's upload file type and data size limit. I would like to know for the whole site or individual forms( i have like 8 different forms)
I would like to set each file for max 5Mb and accept only jpg,png,gif,pdf,doc. and if the user try to
upload something other than this scope, I would like to
display an error message.
I think this is very important and basic feature for wp toolset form. But, why isn't it a basic feature??
I really don't get it though.
I try to use some code snippets from other posts. so far unsuccessful.
Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-limit-the-file-upload-size-limit-from-the-cred-form/
Is there a similar example that we can see?
What is the link to your site?
Hi,
Thank you for contacting us and I'd be happy to assist.
To apply validation on the uploaded file's type and size, you can use the "cred_form_validate" hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
You'll find a good example of this hook's usage in this reply:
https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1411409
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hello Waqar,
Thank you for information.
I copy the code from the link you gave me and modify it as below.
If I understand this correctly, it should limit for form id 1311 upload only PDF upto 5mg for a field "financial-info", right?
I use codesnipet plugin and added there. But it seems not working. I was able to upload 6mb pdf, and some jpeg as well.
Could you explain this little more?
add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
// Field data are field values and errors
list($fields,$errors)=$field_data;
//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 1311 == $form_data['id']) && (isset($_FILES['wpcf-financial-info']['type'])) && (isset($_FILES['wpcf-financial-info']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-financial-info']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-financial-info']['size'];
//Validate files uploaded, make sure its PDF file type AND not more than 5000kB
if (!( ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 5000000) )) {
$errors['pers-financial-info']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.';
}
}
//return result
return array($fields,$errors);
}
Thanks for writing back.
I've tested the code that you've shared and it worked as expected on my test website. I just had to fix this line:
$errors['pers-financial-info']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.';
To:
$errors['financial-info']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.';
Also, this file validation approach will only work if the option "Use the WordPress Media Library manager for image, video, audio, or file fields" is not checked in the form's settings.
Is there anyway to change what's written on my original post? I would like to hide my site name from this post. Could you delete it? also, Please delete this reply aswell after deleting my site name if possible?