Allow JSON files to be uploaded in the media library in WooCommerce on child sites.
Snippet Type
Execute on Child Sites
Snippet
add_filter( 'upload_mimes', 'bbloomer_custom_mime_types' );
function bbloomer_custom_mime_types( $mimes ) {
if ( current_user_can( 'manage_woocommerce' ) ) {
$mimes['txt'] = 'text/plain';
$mimes['json'] = 'text/plain';
}
return $mimes;
}
add_filter( 'wp_check_filetype_and_ext', 'wc_correct_filetypes', 10, 5 );
function wc_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) {
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
return $data;
}
$wp_file_type = wp_check_filetype( $filename, $mimes );
if ( 'json' === $wp_file_type['ext'] ) {
$data['ext'] = 'json';
$data['type'] = 'text/plain';
} elseif ( 'txt' === $wp_file_type['ext'] ) {
$data['ext'] = 'txt';
$data['type'] = 'text/plain';
}
return $data;
}