Hello everyone,
Thought I would share this here for future reference, and/or as feedback.
I think I managed to add working SVG support to the Gridder, by following these steps:
- Add SVG Support plugin to your WP.
This just makes sure you’re actually able to see the SVGs when you add them to your Media Library, as well as be able to add them as images in the Gridder.
- Add the solution offered by Laxmana on this page to your theme’s functions.php file (just copy paste his code to the bottom of your theme’s functions.php file).
function svg_meta_data($data, $id){
$attachment = get_post($id); // Filter makes sure that the post is an attachment
$mime_type = $attachment->post_mime_type; // The attachment mime_type
//If the attachment is an svg
if($mime_type == 'image/svg+xml'){
//If the svg metadata are empty or the width is empty or the height is empty
//then get the attributes from xml.
if(empty($data) || empty($data['width']) || empty($data['height'])){
$xml = simplexml_load_file(wp_get_attachment_url($id));
$attr = $xml->attributes();
$viewbox = explode(' ', $attr->viewBox);
$data['width'] = isset($attr->width) && preg_match('/\d+/', $attr->width, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[2] : null);
$data['height'] = isset($attr->height) && preg_match('/\d+/', $attr->height, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[3] : null);
}
}
return $data;
}
add_filter('wp_update_attachment_metadata', 'svg_meta_data', 10, 2);
What this beautiful piece of code does, is make sure the dimensions of your SVGs are read when you upload your SVGs to the Media Library, and adds this to the file’s info.
Lay Theme needs these dimensions to determine the aspect ratio of your images (regardless of their file type), which in turn is needed to correctly determine how much space these images are taking on your website.
- You can now upload your SVGs to your Media Library, and start adding them to the Gridder as images.
Enjoy!
@mariusjopen @arminunruh
-
Could you please confirm this is a working solution, and that it doesn't break anything? I am by no means a coder…
-
If this is a working solution, maybe you could consider adding this function to the theme?