Skip to content

Instantly share code, notes, and snippets.

@maksimerohin
Last active February 1, 2021 04:38
Show Gist options
  • Save maksimerohin/87a90e9083f35a34ecd5cea876892b38 to your computer and use it in GitHub Desktop.
Save maksimerohin/87a90e9083f35a34ecd5cea876892b38 to your computer and use it in GitHub Desktop.
function mero_myme_types($mime_types){
$mime_types['svg'] = 'image/svg+xml';
return $mime_types;
}
add_filter('upload_mimes', 'mero_myme_types', 1, 1);
# Исправление MIME типа для SVG файлов.
function fix_svg_mime_type( $data, $file, $filename, $mimes, $real_mime = '' ){
// WP 5.1 +
if( version_compare( $GLOBALS['wp_version'], '5.1.0', '>=' ) )
$dosvg = in_array( $real_mime, [ 'image/svg', 'image/svg+xml' ] );
else
$dosvg = ( '.svg' === strtolower( substr($filename, -4) ) );
// mime тип был обнулен, поправим его
// а также проверим право пользователя
if( $dosvg ){
// разрешим
if( current_user_can('manage_options') ){
$data['ext'] = 'svg';
$data['type'] = 'image/svg+xml';
}
// запретим
else {
$data['ext'] = $type_and_ext['type'] = false;
}
}
return $data;
}
add_filter( 'wp_check_filetype_and_ext', 'fix_svg_mime_type', 10, 5 );
# Формирует данные для отображения SVG как изображения в медиабиблиотеке.
function show_svg_in_media_library( $response ) {
if ( $response['mime'] === 'image/svg+xml' ) {
// С выводом названия файла
$response['image'] = [
'src' => $response['url'],
];
}
return $response;
}
add_filter( 'wp_prepare_attachment_for_js', 'show_svg_in_media_library' );
# Добавляет SVG в список разрешенных для загрузки файлов.
function svg_upload_allow( $mimes ) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'svg_upload_allow' );
# Исправление MIME типа для SVG файлов.
function fix_svg_mime_type( $data, $file, $filename, $mimes, $real_mime = '' ) {
// WP 5.1 +
if ( version_compare( $GLOBALS['wp_version'], '5.1.0', '>=' ) ) {
$dosvg = in_array( $real_mime, [ 'image/svg', 'image/svg+xml' ] );
} else {
$dosvg = ( '.svg' === strtolower( substr( $filename, - 4 ) ) );
}
// mime тип был обнулен, поправим его
// а также проверим право пользователя
if ( $dosvg ) {
// разрешим
if ( current_user_can( 'manage_options' ) ) {
$data['ext'] = 'svg';
$data['type'] = 'image/svg+xml';
} // запретим
else {
$data['ext'] = $type_and_ext['type'] = false;
}
}
return $data;
}
add_filter( 'wp_check_filetype_and_ext', 'fix_svg_mime_type', 10, 5 );
# Формирует данные для отображения SVG как изображения в медиабиблиотеке.
function show_svg_in_media_library( $response ) {
if ( $response['mime'] === 'image/svg+xml' ) {
// С выводом названия файла
$response['image'] = [
'src' => $response['url'],
];
}
return $response;
}
add_filter( 'wp_prepare_attachment_for_js', 'show_svg_in_media_library' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment