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.
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 edited Apr 3, 2012 at 18:43 dnagirl asked Mar 7, 2012 at 17:07 dnagirldnagirl 3072 gold badges4 silver badges15 bronze badges3 Answers
Reset to default 2The 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;
}
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.
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.