Skip to main content

How to add a custom file format for uploader?

REQUIREMENTS

To be able to implement this guide, you need to learn how to insert PHP snippets to your website.

You can find guide here: WP Beginner

Better Messages file uploader rely on WordPress file uploader and media gallery to ensure good compatibility with WordPress plugins ecosystem.

To add a custom file format, you need to register it with special php filters. For example, if you want to add .emb format, the code will look like that:

<?php
add_filter( 'ext2type', 'bm_custom_extensions' );

function bm_custom_extensions( $mimes ){
$mimes['interactive'][] = 'emb';
return $mimes;
}

add_filter( 'mime_types', 'bm_custom_upload_mimes', 10, 1 );
function bm_custom_upload_mimes( $existing_mimes ) {
$existing_mimes['emb'] = 'application/octet-stream';
return $existing_mimes;
}

add_filter('wp_check_filetype_and_ext', 'bm_custom_filetype_check', 10, 4);
function bm_custom_filetype_check($data, $file, $filename, $mimes) {
$filetype = wp_check_filetype( $filename, $mimes );

if( $filetype['ext'] && strtolower($filetype['ext']) == 'emb' ) {
$filetype['type'] = 'application/octet-stream';

return [
'ext' => $filetype['ext'],
'type' => $filetype['type'],
'proper_filename' => $data['proper_filename']
];
} else {
return $data;
}
}

This code is just an example. For other extensions, you will need to modify this code, based on your requirements. After this you will be able to enable this file format in Better Messages Attachments settings and be able to upload this type of files.