1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Filter file types
  1. Home
  2. Knowledge Base
  3. File Uploads
  4. Filter file types
  1. Home
  2. Knowledge Base
  3. Advanced
  4. Filter file types

Filter file types

If you’re using the file upload field type, you can change the permitted file types by using a filter. Please remember that allowing users to upload files to your server carries a security risk, so please use this feature with caution.

<?php
// Add PDFs to list of permitted mime types
function my_prefix_pewc_get_permitted_mimes( $permitted_mimes ) {
// Add PDF to the list of permitted mime types
$permitted_mimes['pdf'] = "application/pdf";
// Remove a mime type - uncomment the line below if you wish to prevent JPGs from being uploaded
// unset( $permitted_mimes['jpg|jpeg|jpe'] );
return $permitted_mimes;
}
add_filter( 'pewc_permitted_mimes', 'my_prefix_pewc_get_permitted_mimes' );
// Add PDF to the list of restricted filetypes
function my_prefix_pewc_protected_directory_allowed_filetypes( $restricted_filetypes ) {
$restricted_filetypes[] = 'pdf';
return $restricted_filetypes;
}
add_filter( 'pewc_protected_directory_allowed_filetypes', 'my_prefix_pewc_protected_directory_allowed_filetypes' );

Just add the above code and include the file types you wish to allow.

Note that the code above works for file types that are natively supported by WordPress. If you want to upload files types that aren’t already supported by WordPress, you can filter upload_mimes:

function my_prefix_upload_mimes( $mimes ) {
// Add PhotoShop PSD files to list of permitted WordPress mime types
$mimes['psd'] = "application/x-photoshop";
return $mimes;
}
add_filter( 'upload_mimes', 'my_prefix_upload_mimes' );

You can add this code snippet to your site by following these steps.

Was this article helpful?

Need More Support?

If you can't find the answer that you're looking for, then please use the link below to raise a support ticket.
Contact Support

Related Articles