Skip to content

Instantly share code, notes, and snippets.

@deckerweb
Forked from rmpel/wp-499-up-fix-csv-upload.php
Created December 18, 2018 22:13
Show Gist options
  • Save deckerweb/ab49aa3f7c1f7f7ab50d88485f06dac8 to your computer and use it in GitHub Desktop.
Save deckerweb/ab49aa3f7c1f7f7ab50d88485f06dac8 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment