I am trying to use CRED to allow front end submission of custom posts with a File Custom field to enable uploads of files. I would like to limit the type and size of the upload file. Is there a way to restrict the file type to just PDF and PPT and limit the maximum size of each uploaded file to let us say 10MB ? This should be independent of what type and size of file one can upload from the back end using the Media Uploader.
Also, there is no visual clue as to what is happening when you submit a CRED form with a large file uploaded (unlike using the Backend media uploader where you get the progress bar showing each file upload progress). One gets the impression that nothing is happening and that the submission is not working.
Thanks.
Dear Amr,
In the latest version of CRED (version 1.3.3) , it's possible you can validate the file size and file type using CRED API: https://toolset.com/documentation/user-guides/cred-api/ (cred_form_validate filter). This filter hook allows you to validate the file uploads and show error messages to the user. In case of error, the post will be not be saved.
Below is a working example in my localhost:
<?php
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 (( 7 == $form_data['id']) && (isset($_FILES['wpcf-the-file-field']['type'])) && (isset($_FILES['wpcf-the-file-field']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-the-file-field']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-the-file-field']['size'];
//Validate files uploaded, make sure its PDF file type AND not more than 100kB
if (!( ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 100000) )) {
//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['the-file-field']='Sorry the file you have uploaded is not of correct type or exceeded 100kB limit.';
}
}
//return result
return array($fields,$errors);
}
To use the above code, take note of the following:
1.) Replace 7 with in:
with your actual CRED form ID accepting the file uploads on the front end.
2.) Replace all instances "wpcf-the-file-field" in the code and replace that with your actual custom field name. Make sure you append the wpcf-prefix to this custom field name. If your custom field name is "my-file-field". Then replace "wpcf-the-file-field" with "wpcf-my-file-field".
3.) The above code example validates PDF only. Feel free to edit or add more mimes that you would like validate. There is some complete list or mime types here: hidden link
4.) The file size checking is in terms of bytes, for example 100000. Feel free to edit this value.
5.) Replace "the-file-field" in:
$errors['the-file-field']
With your actual Types file custom field slug. Do not append wpcf- prefix on this. The above code is working well on our development server. It should work well to you too 🙂 Please let me know how it goes. Thanks!
Cheers,
Emerson
Thanks a lots for the solution you provided. It works like a charm.
One question ? If have 2 different cred forms, one for Adding a post and one for editing a post with obviously 2 different ids . Do I need to create 2 different functions with the same code to be able to get both CRED form to do the validation ?
Thanks