Fancy bitmaps are the one thing, scalable pictures the other. WordPress does not like Scalable Vector Graphics by default: SVGs consist of XML code. If loaded, it can — at least in principle — inject malicious code into the system. Using SVGs in templates, however, is not prevented by WordPress. On HTML level, they can be embedded — as usual — in img
tags. That’s the way, also bootScore integrates the logos into the file header.php
. But for the ambitious bootScore user, Fancy SVGs need more:
The svg
tag is used to embed the SVG-code itself into a page.1 But for using SVGs in the media library and putting them into an img
tag, she needs a plugin like SVG Support, WebP SVG Support or Save SVG:
Solution
- Install the plugin Safe SVG
- Load your SVG into your media library.
- Use it, like ‘normal’ images.2
Background
The WordPress plugin SVG-Support promises to ‘sanitize’ the XML code on upload, is actively maintained, and is licensed under GPL‑2.0‑or-later . This also applies to the plugin Safe SVG.3
WebP SVG Support — on the other hand — says nothing about sanitizing the code. But it mentions the media library. Also, Safe SVG promotes itself by its use of the WordPress Media Library, Hence, I prefer to install the plugin Safe SVG.
Because WordPress enables the developers to incorporate vector and raster graphics by the same technique, we can use our ShortCode for ‘Fancy Images’ for SVGs as well:
Nevertheless, I would like to use the CC-BY-SA logo in the footer as part of a line: . Therefore, I need a second version of that function:
/*
* Simple short code for inserting an inline img
* (C) 2023 Karsten Reincke
* SPDX-License-Identifier: MIT
*/
function fciPicCode($atts){
$atts = (array) $atts;
$imid = $atts[0]; // obligatoric parameter
$width = $atts[1]; // obligatoric parameter
$wpsize = ""; // thumbnail, medium, large, or full
$alt=""; // optional parameter
$style="is-style-default"; // optional parameter
/* IDEA: try to download the best fitting image size be default.
* Download the full sized image if and when the user had said
* she wants to see it by a click (fancy)
*/
if ($width <= 150)
$wpsize = "thumbnail";
elseif ($width <=300)
$wpsize = "medium";
elseif ($width <= 1024)
$wpsize = "large";
else
$wpsize = "full";
/* get the path to the original image */
$fimgp=wp_get_attachment_image_url($imid,"full");
if (!($fimgp)) return ("wrong image data");
/* if it's an svg, there won't be any smaller images */
if (wp_get_image_mime($fimgp) == "svg" )
$simgp=$fimgp;
else {
/* otherwise get the path to the fitting smaller image */
$simgp=wp_get_attachment_image_url($imid,$wpsize);
if (!($simgp)) $simgp=$fimgp;
}
if (array_key_exists(2,$atts)) {
$alt=$atts[2];
if (array_key_exists(3,$atts)) {
$style=$atts[3];
}
}
$res=
'<a href="' . $fimgp . '" data-fancybox="">' .
'<img src="' . $simgp . '" alt="'. $alt .'" width="' . $width . '" >' .
'</a>' ;
return $res;
}
add_shortcode('fcipic', 'fciPicCode');
And how does this …
… support our migration to bootScore? Well, once a web designer has taken the first general steps and has checked SEO, she will soon turn to a really thick board, her properly clustered and presented menu without getting too fancy. Eventually, she is going to spruce and speed up the display of fixed and scalable images. This post talks about a part of this way.
- see W3C-Schools [↩]
- WordPress embeds it via an IMG tag [↩]
- For code sanitization, see description, for licensing the file readme.txt. [↩]