Setting up SVG Fallback Images in WordPress

SVG images are almost 96% supported in browsers but need a bit of backward compatibility for the 4% of stragglers, out of the box WordPress does not support the upload of SVG images via the media loader but you can circumnavigate that by adding the image format to the list of allowed types.

There is a nice javascript plugin called SVGeezy (love that name) that can check if a browser supports SVG and if it doesn’t can fall back to an alternative image in the same directory with a different file type such as .png

Download the latest version of SVGeezy and grab the svgeezy.min.js file and add to your theme’s js folder and enqueue it, also create an init file, here called svgeezy-init.js

In the svgeezy-init.js add the below

What we are saying here is to check images and if SVG is not supported use a .png variation in the same location. Accordingly you can use another format like .jpg

You can also filter which images to check by using a CSS class on the image like so

That’s it, the scripts are set to appear at the bottom of the page, there is no jQuery dependency and the file size is light at 1KB.


Leave all Comment