I am having an issue with restricting file types to PDF and DOCX in a custom form on my WordPress website. The file type validation code I am using does not seem to work.
Solution:
Go to the Form edit screen and uncheck "Use the WordPress Media Library manager for image, video, audio, or file fields". Then, use the following code snippet by adding it to your theme's functions.php file or a custom plugin:
add_filter('cred_form_validate', 'cred_filetype_size_validation', 10, 2);
function cred_filetype_size_validation($field_data, $form_data) {
list($fields, $errors) = $field_data;
if (14 == $form_data['id']) {
$file_type_uploaded = $_FILES['wpcf-documento-obra-escritor']['type'];
if (!('application/pdf' == $file_type_uploaded) || ($file_type_uploaded == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')) {
$errors['documento-obra-escritor'] = 'Sorry, the file you have uploaded is not of the correct type.';
}
}
return array($fields, $errors);
}
Make sure to replace 14 with your form's actual ID.