Problem: I would like to enforce file type restrictions on a file upload field in a Form, but the cred_form_validate hook does not provide any information about the file uploads.
Solution: Use the cred_file_upload_disable_progress_bar hook to inspect AJAX file uploads. Inspect the $_FILES superglobal to retrieve file metadata.
//*Validate for PDF on File Upload add_filter('cred_form_ajax_upload_validate','cred_filetype_validation',10,2); function cred_filetype_validation($error_fields, $form_data) { $file_types = array('application/pdf','image/jpeg','image/png','image/tiff','image/gif'); // Field data are field values and errors list($fields,$errors)=$error_fields; //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set. if (in_array($form_data['id'], [654,836,848]) && (isset($_FILES['wpcf-pedigree']['type']))) { //Retrieve file type $file_type_uploaded=$_FILES['wpcf-pedigree']['type']; //Validate files uploaded, make sure its PDF file type if (!in_array($file_type_uploaded, $file_types) ) { //Validation failed, // set error message per field //Remove wpcf - prefix here! $errors['pedigree']='Sorry the file you have uploaded is not of the correct type. Accepted file types: pdf, jpg, gif, png, tiff'; } } //return result return array($fields,$errors); }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
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.
Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
Este tema contiene 5 respuestas, tiene 2 mensajes.
Última actualización por hace 6 años, 5 meses.
Asistido por: Christian Cox.