Zum Hauptinhalt springen
Blog & News

Aktuelles & Updates von
Joomla, Wordpress und Co.

News, HowTos und Tutorials rund um Webdesign, Webentwicklung, Webhosting und Digitalisierung.

Blog & News

WordPress SVG width height ist Null

15.03.2022 · 2 Min Lesezeit

SVG Bilder werden zwar erfolgreich in Wordpress eingebunden, dann aber mti "weight="0" und height="0"" ausgegeben.

The error and code: evidence of zero-width

 

This is the error that Wordpress throws on the page: Warning: Division by zero in /wp-content/themes/myTheme/functions.php on line 771

Hier der funktionierende Filter:

add_filter( 'wp_get_attachment_image_src', 'fix_wp_get_attachment_image_svg', 10, 4 ); /* the hook */

function fix_wp_get_attachment_image_svg($image, $attachment_id, $size, $icon) {
if (is_array($image) && preg_match('/\.svg$/i', $image[0]) && $image[1] <= 1) {
if(is_array($size)) {
$image[1] = $size[0];
$image[2] = $size[1];
} elseif(($xml = simplexml_load_file($image[0])) !== false) {
$attr = $xml->attributes();
$viewbox = explode(' ', $attr->viewBox);
$image[1] = isset($attr->width) && preg_match('/\d+/', $attr->width, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[2] : null);
$image[2] = isset($attr->height) && preg_match('/\d+/', $attr->height, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[3] : null);
} else {
$image[1] = $image[2] = null;
}
}
return $image;
}

 

Quelle: https://wordpress.stackexchange.com/questions/255314/cant-extract-and-set-svg-dimensions

Zurück zur Übersicht