Skip to content

Instantly share code, notes, and snippets.

@leurdo
Created February 27, 2017 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leurdo/92e89a3247b2cdf06f5e80e8af6eea7e to your computer and use it in GitHub Desktop.
Save leurdo/92e89a3247b2cdf06f5e80e8af6eea7e to your computer and use it in GitHub Desktop.
Как разрешить загрузку svg в вордпресс 4.7.1
/**
*
* SVG upload fix for wp 4.7.1
*
* @link https://wordpress.org/support/topic/wp-4-7-1-kills-svg/page/4/
*
*/
add_filter('wp_check_filetype_and_ext', 'ignore_upload_ext', 10, 4);
function ignore_upload_ext($checked, $file, $filename, $mimes)
{
//we only need to worry if WP failed the first pass
if(!$checked['type']){
//rebuild the type info
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
$proper_filename = $filename;
//preserve failure for non-svg images
if($type && 0 === strpos($type, 'image/') && $ext !== 'svg'){
$ext = $type = false;
}
//everything else gets an OK, so e.g. we've disabled the error-prone finfo-related checks WP just went through. whether or not the upload will be allowed depends on the <code>upload_mimes</code>, etc.
$checked = compact('ext','type','proper_filename');
}
return $checked;
}
/**
* Add file support for media
*
*/
function svg_myme_types($mime_types){
$mime_types['svg'] = 'image/svg+xml'; //Adding svg extension
return $mime_types;
}
add_filter('upload_mimes', 'svg_myme_types', 1, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment