Take a look at wp_prepare_attachment_for_js()
, which is what gathers attachment metadata for use on the Media pages. The eponymous filter lets us add or alter metadata.
The following example can be dropped into functions.php. Note: this requires SimpleXML support in PHP.
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);