How to add a custom file format for uploader?
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 relies on the WordPress file uploader and media gallery to ensure good compatibility with the WordPress plugin ecosystem.
This means a format has to be known to WordPress before Better Messages can offer it. Adding a format is a two-part job:
- Register the extension with WordPress using PHP filters (the snippet below)
- Enable the format in Better Messages → Settings → Attachments
Both parts are required. A format registered in PHP but left unchecked in the Attachments settings will still be rejected, because Better Messages only permits the extensions selected there.
Register the extension#
The ext2type filter puts the extension into a group, which controls where its checkbox appears in the Attachments settings. The mime_types filter is what actually allows the upload.
For example, to add the .apk format:
<?php
add_filter( 'ext2type', 'bm_custom_extensions' );
function bm_custom_extensions( $types ) {
$types['archive'][] = 'apk';
return $types;
}
add_filter( 'mime_types', 'bm_custom_upload_mimes' );
function bm_custom_upload_mimes( $existing_mimes ) {
$existing_mimes['apk'] = 'application/vnd.android.package-archive';
return $existing_mimes;
}
Then go to Better Messages → Settings → Attachments and tick the new format in the list. It appears under the group you chose in ext2type — archive in the example above.
Valid groups are image, audio, video, document, spreadsheet, interactive, text and archive.
Do not use the code group. It exists in WordPress, but Better Messages deliberately hides it from the Attachments settings, so an extension registered there will never get a checkbox and can never be enabled.
Use the format's real MIME type when it has one. If the format has no registered MIME type, application/octet-stream is a safe generic fallback.
When the upload is still rejected#
WordPress inspects the actual contents of an uploaded file and compares the detected MIME type against the one you declared. If the two disagree, the upload is refused even though both filters above are in place.
Most formats pass without any extra work. WordPress is lenient when the detected type is a generic binary type such as application/zip or application/octet-stream, and .apk files are accepted with just the two filters above. But an obscure format that the server misidentifies as some third, unrelated type will be blocked.
If your format is rejected after registering it, add the following filter to skip content inspection for that extension:
<?php
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 ( ! empty( $filetype['ext'] ) && strtolower( $filetype['ext'] ) === 'apk' ) {
return array(
'ext' => $filetype['ext'],
'type' => 'application/vnd.android.package-archive',
'proper_filename' => $data['proper_filename'],
);
}
return $data;
}
This filter tells WordPress to trust the file extension instead of verifying the contents. Only add it for formats you actually need, and keep the extension check in place so other uploads are still validated normally.
Allowing a format for specific users only#
Executable formats such as .apk are worth restricting. The bp_better_messages_attachment_allowed_extensions filter receives the allowed extension list along with the thread and user, so you can widen it for some people only.
Leave the format unchecked in the Attachments settings and add it per user instead:
<?php
add_filter( 'bp_better_messages_attachment_allowed_extensions', 'bm_extensions_per_user', 10, 3 );
function bm_extensions_per_user( $extensions, $thread_id, $user_id ) {
if ( $user_id > 0 && user_can( $user_id, 'manage_options' ) ) {
$extensions[] = 'apk';
}
return $extensions;
}
The $user_id is negative for guest users, so the > 0 check above also keeps guests from uploading the format.
Other formats#
The examples use .apk, but the same three filters work for any extension. Replace the extension, the MIME type and the group to match your format. For instance, a custom .emb design file with no official MIME type:
<?php
add_filter( 'ext2type', function ( $types ) {
$types['document'][] = 'emb';
return $types;
} );
add_filter( 'mime_types', function ( $mimes ) {
$mimes['emb'] = 'application/octet-stream';
return $mimes;
} );