December 30, 2024
1 min read

We can use SVG image in <img> tag but with that method, we cant use javascript animation or CSS style on that SVG. So the idea is we will put our SVG image as HTML image tag for example

<img src="logo.svg"/ >

And we will get out with a <svg> tag.

Before we get into it, wordpress dont allow upload svg file by defult. so we need to put a line of code on wp-config.php

wp-config.php
/* Allow Unfiltered Upload. /
define('ALLOW_UNFILTERED_UPLOADS', true);

Now need put this code on functions.php

functions.php
/** add SVG to allowed file uploads **/
function agora_custom_mime_types( $mimes ) {
	// New allowed mime types.
	$mimes['svg'] = 'image/svg+xml';
	$mimes['svgz'] = 'image/svg+xml';
	$mimes['doc'] = 'application/msword';
	 
	// Optional. Remove a mime type.
	unset( $mimes['exe'] );
	 
	return $mimes;
}
add_filter( 'upload_mimes', 'agora_custom_mime_types' );


Now the final part JavaScript

custom.js
/** Image to SVG **/
// chage img tag class to svg or other
$('img.svg').each(function(){
var $img = $(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');

$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');

// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}

// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');

// Check if the viewport is set, else we gonna set it if we can.
if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
$svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
}

// Replace image with new SVG
$img.replaceWith($svg);

}, 'xml');

});

That’s all. Make sure you have svg class on your img tag or chnage your JavaScript code

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

Remove WooCommerce checkout fields

Remove WooCommerce Checkout field with simple filter hook

Get WooCommerce product info

Show product info in the place as you wish to. It help you to make your custom woocommerce product page layout.

Remove Website field from WordPress comment & Change cookies remember text

Remove Website field from WordPress comment & Change cookies remember text to Remember me!

Create a new WordPress administrator via functions.php & FTP

Sometimes, you might need to create an administrator account in WordPress without being able to access the admin dashboard. This could be because you have lost access to your site’s admin panel or when troubleshooting a client’s website. In this tutorial, we will show you how to programmatically add a WordPress administrator account using the […]

Web Development Project in mind?

if you looking for a web developer for paid contribution to your project I am available for work.

Mukto
Mukto

Click the button below to chat with me.