mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-30 11:42:22 +08:00
26 lines
809 B
Text
26 lines
809 B
Text
add_filter( 'upload_mimes', 'wc_custom_mime_types' );
|
|
|
|
function wc_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;
|
|
}
|