Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
It turns out that my filter is not the problem. The issue is a conflict with a backup plugin. When the plugin is off, my filter works fine. Thanks though. –  dnagirl Apr 3 '12 at 18:40
1  
this will kill all allowed mime types currently set by WP. Probably not a good idea. –  chrisguitarguy Oct 13 '12 at 6:21
    
Maybe use $mime_types .= array(, so it appends them to the default ones. –  Ciprian Popescu Nov 13 '12 at 9:19
    
@CiprianPopescu $mime_types += ... –  kaiser Nov 13 '12 at 15:08
    
@kaiser += is for Javascript, .= is for PHP. –  Ciprian Popescu Nov 14 '12 at 10:45
show 2 more comments

The problem is with the filter. Deregister the old key, to avoid conflicts. Then simply add your new one.

add_filter( 'upload_mimes', 'wpse44777_upload_mimes' );
function wpse44777_upload_mimes( $mime_types )
{
    // First we unregister the old key
    unset( $mime_types['txt|asc|c|cc|h'] );

    // Then we add a new one
    ! isset( $mime_types['txt|asc|c|cc|h|wif'] ) AND $mime_types['txt|asc|c|cc|h|wif'] = 'text/plain';

    return $mime_types;
}
share|improve this answer
    
Other than specifying the array index rather than searching for it, how is this different from my 2nd attempt (if($ek=array_search($v,$mimes)){...)? I unset the existing array member and then I add my own and return the array. And yet it only works if I turn off BackUpWordPress. –  dnagirl Nov 13 '12 at 13:42
    
@dnagirl Have overseen your unset(). In this case, it's simple: Blame the plugin. And yes, yours isn't really different. Just a little less readable and slower :) Please add your solution (if you found out why BackUpWordPress was causing this issue) and maybe report that to the author. –  kaiser Nov 13 '12 at 14:11
    
unfortunately, I haven't discovered why the plugin was causing the problem. Their codebase does not even touch the upload_mimes() filter. But if I deactivate it, all is good. BTW, the reason I used the loop was in case WP ever changed their key for 'text/plain'. –  dnagirl Nov 13 '12 at 15:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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