I wanted to add support for .wif files (text/plain) to my site (WP3.3.1). So I added the below function to my theme’s functions.php. My theme is a child theme of Suffusion.

add_filter('upload_mimes', 'custom_upload_mimes');

function custom_upload_mimes ( $existing_mimes=array() ) {
  $existing_mimes['wif'] = 'text/plain';
  return $existing_mimes;
}

It had no effect and .wif files continued to generate the security message when I tried to upload them.

So I looked at the source code for get_allowed_mime_types(). The only entry for ‘text/plain’ is this 'txt|asc|c|cc|h' => 'text/plain'. Just for fun I edited it to say this: 'txt|asc|c|cc|h|wif' => 'text/plain'. That edit allowed .wif files to be uploaded.

But since editing core WP files is a bad idea, I tried another solution. On the assumption that perhaps values in the $mimes array had to be unique, I tried changing the key that points to ‘text/plain’ with this function:

add_filter('upload_mimes', 'custom_upload_mimes');

function custom_upload_mimes($mimes=array()){
    $k='wif';
    $v='text/plain';
    if($ek=array_search($v,$mimes)){
        unset($mimes[$ek]);
        $ek.='|'.$k;
        $mimes[$ek]=$v;
    }
    return $mimes;
}

This, however, also does not allow .wif file upload.

So I think that either the upload_mimes filter is not being applied for some reason. Or, my filter is being overwritten by another. I’ve tried both my functions with high (1) and low (PHP_MAX_INT) priority. It has no effect. I also checked for .htaccess directives and their weren’t any. Any ideas?

ETA
It turns out that some combination of plugins that includes BackUpWordPress causes filters attached to ‘upload_mimes’ to not run. I have not determined why this is and the people oat BackUpWordPress tell me that their plugin does not touch that filter.

3 Answers
3

I would use:

add_filter( 'upload_mimes', 'theme_restrict_mime_types' );
function theme_restrict_mime_types( $mime_types )
{
    $mime_types = array(
        'wif' => 'text/plain',
        'doc|docx' => 'application/msword',
        'jpg|jpeg' => 'image/jpeg',
        'gif' => 'image/gif',
        'png' => 'image/png'
    );
    return $mime_types;
}

In this example I list all types that I allow (with WIF included). So you would need to add whats missing for your liking.

This works on my WP 3.3.1 install.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *