Skip to content

Instantly share code, notes, and snippets.

@Gkiokan
Created April 5, 2017 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gkiokan/df02ba990d0cd7be2539dd9f9dff78f9 to your computer and use it in GitHub Desktop.
Save Gkiokan/df02ba990d0cd7be2539dd9f9dff78f9 to your computer and use it in GitHub Desktop.
WP recognize the SVG as Image in Previews
<?php
/**
* File: This enables you svg support fot the media preview in WP.
*
***/
function common_svg_media_thumbnails($response, $attachment, $meta){
if($response['type'] === 'image' && $response['subtype'] === 'svg+xml' && class_exists('SimpleXMLElement'))
{
try {
$path = get_attached_file($attachment->ID);
if(@file_exists($path))
{
$svg = new SimpleXMLElement(@file_get_contents($path));
$src = $response['url'];
$width = (int) $svg['width'];
$height = (int) $svg['height'];
//media gallery
$response['image'] = compact( 'src', 'width', 'height' );
$response['thumb'] = compact( 'src', 'width', 'height' );
//media single
$response['sizes']['full'] = array(
'height' => $height,
'width' => $width,
'url' => $src,
'orientation' => $height > $width ? 'portrait' : 'landscape',
);
}
}
catch(Exception $e){}
}
return $response;
}
add_filter('wp_prepare_attachment_for_js', 'common_svg_media_thumbnails', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment