Skip to content

Instantly share code, notes, and snippets.

@rmpel
Last active January 2, 2022 15:01
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rmpel/e1e2452ca06ab621fe061e0fde7ae150 to your computer and use it in GitHub Desktop.
Save rmpel/e1e2452ca06ab621fe061e0fde7ae150 to your computer and use it in GitHub Desktop.
A filter (an mu-plugin) to restore CSV upload functionality to WordPress 4.9.9 and up.
<?php
/**
* Restore CSV upload functionality for WordPress 4.9.9 and up
*/
add_filter('wp_check_filetype_and_ext', function($values, $file, $filename, $mimes) {
if ( extension_loaded( 'fileinfo' ) ) {
// with the php-extension, a CSV file is issues type text/plain so we fix that back to
// text/csv by trusting the file extension.
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$real_mime = finfo_file( $finfo, $file );
finfo_close( $finfo );
if ( $real_mime === 'text/plain' && preg_match( '/\.(csv)$/i', $filename ) ) {
$values['ext'] = 'csv';
$values['type'] = 'text/csv';
}
} else {
// without the php-extension, we probably don't have the issue at all, but just to be sure...
if ( preg_match( '/\.(csv)$/i', $filename ) ) {
$values['ext'] = 'csv';
$values['type'] = 'text/csv';
}
}
return $values;
}, PHP_INT_MAX, 4);
@ymladenov
Copy link

Thanks!

@RadGH
Copy link

RadGH commented Jul 25, 2019

I had the same issue but the CSV being imported had some HTML in it. I assume that is the reason why the "$real_mime" was "text/html", and this fix didn't work. Strangely we've used dozens of CSV files before and only recently had this become an issue.

I changed line 13 to include: $real_mime === 'text/plain' || $real_mime === 'text/html'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment