3

My wp-config.php already has

define('ALLOW_UNFILTERED_UPLOADS', true);

But when I upload a .nb file, it shows "This file type is not allowed. Please try another"

How do I allow to upload any type of files? My WP version is 4.7.3.

2

Original Answer

Add the code below to your current theme's functions.php:

function wpse_add_nb_mime_type( $allowed_mimes ) {
    $allowed_mimes['nb'] = 'application/mathematica';
    return $allowed_mimes;
}
add_filter( 'mime_types', 'wpse_add_nb_mime_type', 10, 1 );

Remove define, mentioned in your question, from wp-config.php. Now you should be able to upload .nb files.

Plugin alternative: to make the code above, immune to theme changes, it's better to put it into a plugin. Create new file wpse-addnbmimetype.php, with this content:

<?php
/*
Plugin Name: WPSE Add NB Mime Type
Description: Adds 'nb' => 'application/mathematica' to allowable mime types
*/
function wpse_add_nb_mime_type( $allowed_mimes ) {
    $allowed_mimes['nb'] = 'application/mathematica';
    return $allowed_mimes;
}

function wpse_prepare_add_nb_mime_type() {
    // this filter can be added as early as in init action
    add_filter( 'mime_types', 'wpse_add_nb_mime_type', 10, 1 );
}
add_action( 'init', 'wpse_prepare_add_nb_mime_type' );

Put this file into '/wp-content/mu-plugins/' folder. Now you can switch themes, without losing ability to upload .nb files.

Analysis

Read comments to @majick's answer, first. After all corrections, his filter will look like this:

add_filter('upload_mimes', 'modify_upload_mimes', 10, 2);
function modify_upload_mimes($t, $user) {
    if ($user == null) {$user = wp_get_current_user();}
    if (in_array('administrator', $user->roles)) {
        $t['nb'] = 'application/mathematica';
    }
    return $t;
}

Does it work? Yes, but it does restrict uploads of files with .nb extensions to 'administrator' role only. Authors and editors will not be able to upload such files. Let's change it, by replacing line:

if (in_array('administrator', $user->roles)) {

with:

if ( $user->has_cap( 'upload_files' ) ) {

Now, authors, editors, and administrators can upload .nb files.

Checks against user's role should be discouraged, and should be made against user's capabilities, instead. If one day, we decide to add 'upload_files' capability to contributor's role ( all contributors ), or to the individual user, our filter will work, without changes.

Why do we have two hooks, 'mime_types' and 'upload_mimes', to choose from? It is recommended, to use 'mime_types' for adding mime types, and 'upload_mimes' for removing ( to unset ) them.

There are two functions in '/wp-includes/functions.php': first - get_allowed_mime_types( $user = null ), introducing 'upload_mimes' filter hook, which was intended for arbitrary removal of 'swf', 'exe', and conditional removal of 'htm|html', mime types, and second - wp_get_mime_types(), introducing 'mime_types' filter hook, intended for adding mime types.

Conclusion

Technically, we could use either hook for our solution, but according to recommendations, 'mime_types' should be used. No checks for user's capabilities are necessary, as both hooks will be triggered for users with 'upload_files' capability, only.

2
  • To make this code immune to theme changes, it's probably better, to place it in a plugin. Jun 25 '17 at 10:31
  • You say to create file wpse-addnbmimetype.php in wp-content/mu-plugins/, I have no such directory in my WordPress site. Where should I put it instead? And what, if anything, do I do in order to cause that file to be executed? What should its permissions be?
    – murray
    Aug 2 '20 at 21:43
0

You may be able to debug this by doing the following:

apply_filters('user_has_cap',  'check_user_caps', 10, 4);
function check_user_caps($allcaps, $caps, $args, $user) {
    if (in_array('administrator', $user->roles)) {
        echo "<!-- Admin Caps: "; print_r($allcaps); echo " -->";
    }
    return $allcaps;
}

Then check your page source and look for 'unfiltered_upload' capability. It should be there, if not something is going wrong...

The other way to do this is using the upload_mimes filter. This is probably a better way to go since you can add just the extension(s) you need and so is a much more secure way of going. eg:

add_filter('upload_mimes', 'modify_upload_mimes', 10, 2);
function modify_upload_mimes($t, $user) {
    if ($user == null) {$user = wp_get_current_user();}
    if (in_array('administrator', $user->roles)) {
        $t['nb'] = 'application/mathematica';
    }
    return $t;
}

see function get_allowed_mime_types in wp-includes/functions.php

6
  • in your filter, adding mime type is malformed, therefore it does not work. You're trying to add key only without value. It should be $t['nb'] = 'application/mathematica';. Jun 25 '17 at 1:04
  • upload_mimes filter has one parameter only. Your callback function fails as you try to access $user variable, which is not passed to the function. Jun 25 '17 at 1:24
  • Interesting. Correction to my previous comment. upload_mimes filter has indeed two parameters. Apparently codex has an old documentation, which does not match the source. Sorry. Jun 25 '17 at 3:32
  • yes I didn't check the codex but just the source file so figured using the extra $user parameter worth it for security. fixed the mime type and key now thanks.
    – majick
    Jun 25 '17 at 4:31
  • I've tested your modified filter, and it does not work. Still cannot upload .nb files.The filter from my answer works without a glitch. Note that I use a different hook - mime_types, as recommended by codex and other sources. Jun 25 '17 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.