-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallow-csv-uploads.php
More file actions
29 lines (26 loc) · 930 Bytes
/
allow-csv-uploads.php
File metadata and controls
29 lines (26 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/**
* Restore CSV upload functionality for WordPress 4.9.9 and up
*
* @package dev-toolbox
*/
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 ( in_array( $real_mime, [ 'text/plain', 'text/html' ], true ) && 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 );