Skip to content

Instantly share code, notes, and snippets.

@rmpel
Last active January 2, 2022 15:01
Show Gist options
  • 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);
@stevengliebe
Copy link

Thank you for this. It helped me solve a similar issue with uploads in my plugin.

@Linuxhombre
Copy link

Where should we add this add_filter() option; to functions.php? Please advise. Thanks all!

@Linuxhombre
Copy link

Just need to know what file we need to add the add_filter patch option to as shown in the GIT patch? Thanks much. Just looking for how to apply the patch. I just added it to functions.php but i’m still seeing a CSV upload error inside of media when i drop in a CSV file for testing.

@lukecav
Copy link

lukecav commented Dec 18, 2018

Thanks for the code snippet. This might be fixed in WordPress 5.0.3.

@Linuxhombre
Copy link

@lukecav - did this work for you?

@rmpel
Copy link
Author

rmpel commented Dec 19, 2018

You can create an mu-plugin file with this content, or place it in your theme functions.php, whatever floats your boat :)

@Linuxhombre
Copy link

I'm still getting the same error for some reason. I tested with and without the closing bracket. Could you provide some Guidance? Much appreciated as I know this should be much simpler. I installed the MU plugin successfully but still no results even though I see the plugin live even on the back end. We're just using a standard CSV utility for importing Products and products updates overnight. It is still failing. File type error.

@lukecav
Copy link

lukecav commented Dec 19, 2018

@Linuxhombre

Yep it did work for me in WP 5.0.1 using WooCommerce 3.5.2.

@rmpel
Copy link
Author

rmpel commented Dec 20, 2018

"We're just using a standard CSV utility for importing Products and products updates overnight"
Sorry, this fix is for uploading CSV files to the Media library, I'm not sure why your task fails, but if I understand you correctly, this is not a Media Library problem. I probably could help you out, but I would need much more detailed information, error logs, etc. Access to your system (FTP) would help. I am for hire :) (but due to the Christmas & New Years rush that might be second half of January)

There is a different solution, maybe, probably. If your troubles started with WordPress 4.9.9 or higher, and you had no problem with 4.9.8, just revert. Easiest way to do this is by using the command line, but that would require

  • SSH access to your website hosting (a good hosting company allows this, so if not, time to shop around)
  • WP-CLI software present (should be no problems at all once you have SSH access)
    Then using the wp-cli software, from inside the WordPress directory;
    (this command is elaborate on purpose so it works for everybody, not just for those with the WP-CLI properly set-up)

php path/to/your/copy/of/wp-cli.phar core update --force --version=4.9.8

You should probably disable auto-updates as well, Google is your friend.

-- non-profit promotional text below ;) --

If you are - as I am - as we are at my job - unhappy with the road taken by the WordPress developer team, there is a great alternative; just take a look at ClassicPress.

I am not affiliated with ClassicPress but we will be switching all our WP sites to CP in the near future.
As a lot of people do, we use WordPress for corporate websites mostly and Matt and his cohorts have obviously chosen to serve the hobbyist, not the professionals. That is THEIR choice, I respect that choice. It is unfortunate, however, that the respect is not mutual. There are reports of temporary or even permanent bans on the official WordPress forums for just mentioning the name ClassicPress.

-- promotional text ends --

Back to the problem; if 4.9.8 solves your problem, so will ClassicPress 1.0.0, if 4.9.8 does NOT solve your problemen, so won't ClassicPress.

@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