Allow JSON, CSV and Other Data File Uploads
Lets administrators upload data files such as JSON, CSV, XML and Markdown to the Media Library, which WordPress blocks by default.
Opens WPIDE on
Code
add_filter( 'upload_mimes', function ( $mimes ) {
if ( ! current_user_can( 'manage_options' ) ) {
return $mimes;
}
// Extension => MIME type.
$types = array(
'json' => 'application/json',
'csv' => 'text/csv',
'xml' => 'application/xml',
'md' => 'text/markdown',
);
return array_merge( $mimes, $types );
} );
add_filter( 'wp_check_filetype_and_ext', function ( $data, $file, $filename, $mimes ) {
if ( ! empty( $data['ext'] ) || ! current_user_can( 'manage_options' ) ) {
return $data;
}
$check = wp_check_filetype( $filename, $mimes );
if ( in_array( $check['ext'], array( 'json', 'csv', 'xml', 'md' ), true ) ) {
$data['ext'] = $check['ext'];
$data['type'] = $check['type'];
}
return $data;
}, 10, 4 );
Notes
Only users with the manage_options capability get the extra file types. Edit $types to add or remove extensions.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.