Tell us what you are trying to do?
I'm trying to block users from uploading .heic files. They are not allowed and do not work in another form plugin but in post forms the file extension is able to be uploaded and then causes issues with the website.
Is there any documentation that you are following?
I have not attempted to implement this yet. Looking at the code on the first link, maybe I'm wrong, but it looks like the first if to verify the form should have { } around the rest of the code?
I want all forms with upload options to only allow the allowed wordpress images. Why doesn't toolset abide by what is set in wordpress or if they are going to do this natively on their own, have a spot to set the allowed filetypes vs having to add to the functions.php. Seems it's a step backwards in a tool.
You are right, the code has some errors. I run a test on a clean install and I was able to limit this file extension from uploading. You can login to my test site with the following URL hidden link
The custom code that I used is:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data){
list($fields,$errors) = $error_fields;
// validate if specific form
if ( $form_data['id'] == 6 ) {
$file = $fields['wpcf-my_field']['value'];
$file_arr = explode('.', $file);
if ( end($file_arr) == 'heic' ){
// set error message per field
$errors['my_field']='HEIC files are not allowed';
}
}
// return result
return array($fields,$errors);
}
You can test it here hidden link
Regarding your question Why doesn't toolset abide by what is set in wordpress
It turns out that WordPress does not limit this file extension. I was able to upload it from the WordPress library. I assume that it will need custom code to be restricted in the WordPress media library.
I hope this helps. Let me know if you have any questions.
Ok. I understand that. I'll need to wrap my head around the changes I'll need for my purposes. I have 4 forms, 2 for adding and 2 for editing. One of the forms (add and edit) only has a single image so with just editing the if for the form to include both, this should work fine changing wpcf-my_field to wpcf-profile-picture and my_field to profile-picture. The other form, however, has 2 image fields. One is a normal image field "property-featured-image" but the other, "property-images" has "Allow multiple instances of this field " selected as the user can add as many images as they want. How would I handle the multiple instances? I think I can figure out the rest once I know how to refer to the multiple instances.
Jamal is on vacation. This is Minesh here and I'll take care of this ticket. Hope this is ok.
When you have repeating field, the values will be available as array.
For example - if yo upload two images:
$fields['image_field']['value'][0],
$fields['image_field']['value'][1]
So, you can navigate through all the file name values using for /foreach loop. For example:
foreach($fields['image_field']['value'] as $k=>$v):
$file_arr = explode('.', $v);
if ( end($file_arr) == 'heic' ){
$errors['wpcf-image_field']='One of the uploaded file having HEIC extension. HEIC files are not allowed';
}
endforeach;