5

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

2

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;
}
5
  • 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, 2012 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, 2012 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, 2012 at 15:56
  • Just want to add to make sure you know the mime type of the file you're trying to upload by checking it on your file system (stackoverflow.com/questions/2227182/…). Also if you're using a multisite to make sure you go to Network Admin Area > Settings > Upload Settings in your WordPress multi-site network panel and use the setting called “Upload File Types” where you can add more file extensions which should be allowed on all blogs/sites on the multi-site network. Feb 6, 2018 at 20:01
  • @kaiser - Why do the mime types need to be unset? It isn't also clear how the other approaches impact speed in comparison to you suggest?
    – Motivated
    Nov 6, 2020 at 21:14
1

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.

8
  • 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, 2012 at 18:40
  • 1
    this will kill all allowed mime types currently set by WP. Probably not a good idea. Oct 13, 2012 at 6:21
  • Maybe use $mime_types .= array(, so it appends them to the default ones.
    – Ciprian
    Nov 13, 2012 at 9:19
  • @CiprianPopescu $mime_types += ...
    – kaiser
    Nov 13, 2012 at 15:08
  • @kaiser += is for Javascript, .= is for PHP.
    – Ciprian
    Nov 14, 2012 at 10:45
0

This post is quite old, but I don't think that's the correct mime type any longer for WIF files.

function t4a_add_custom_upload_mimes( $allowed_mimes ) {
    $allowed_mimes['wif'] = 'application/watcherinfo+xml';
    return $allowed_mimes;
}
add_filter( 'upload_mimes', 't4a_add_custom_upload_mimes' );

I will add that this is working locally for me, but not on my live sites so there's some other server conflict that I'm not aware of yet.

2
  • 1
    the .wif files in the original question are text/plain files. They are used in weaving software.
    – dnagirl
    Mar 29, 2020 at 3:49
  • I knew they were weaving files as I was trying to help someone get uploads allowed on their site... but didn't know they were text. iana.org/assignments/media-types/application/watcherinfo+xml suggests the type should be application/watcherinfo+xml but as I've noted this isn't enough to get them uploading again on live sites. Mar 29, 2020 at 23:05

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .