Is there a way to incorporate SVG files into WordPress websites?

After successfully setting up the SVG code I want in JSfiddle, I'm now facing the challenge of loading it into a WordPress page. The HTML and CSS are set up, but I am struggling to integrate it smoothly.

I initially tried adding the CSS to my child theme and pasting the HTML code directly into the editor, but that didn't yield the desired result. I also attempted iframing it and exploring other options, but my limited technical knowledge is proving to be an obstacle.

If anyone has a solution or suggestion on how to successfully incorporate this SVG code into a WordPress page, I would greatly appreciate it.

This is my first time reaching out for assistance here, so please forgive me if I overlooked something obvious.

Code:

.hover_group:hover {
  opacity: 1;
}

#projectsvg {
  position: relative;
  width: 100%;
  padding-bottom: 77%;
  vertical-align: middle;
  margin: 0;
  overflow: hidden;
}

#projectsvg svg {
  display: inline-block;
  position: absolute;
  top: 0;
  left: 0;
}
<figure id="projectsvg">
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1920 1080" preserveAspectRatio="xMinYMin meet">
    //set your background image
<image width="1920" height="1080" xlink:href="https://s1.thcdn.com/design-assets/images/nintendo/en/carousel/10881668/large/10881668-3.jpg">
</image>
<image id="estaca" width="120" height="120" xlink:href="https://portuguesewithcarla.com/temp/estaca.png" x="860" y="250" />
<g class="hover_group" opacity="0">
<a xlink:href="http://ptwithcarla.com">
<text x="760" y="380" font-size="40">NÍVEL UM</text>
<rect x="860" y="250" opacity="0.3" fill="#FFFFFF" width="120" height="120"></rect>
</a>
</g>
<g class="hover_group" opacity="0">
<a xlink:href="http://ptwithcarla.com">
<text x="960" y="680" font-size="40">NÍVEL DOIS</text>
<rect x="922" y="654.1" opacity="0.3" fill="#FFFFFF" width="64.6" height="300.8"></rect>

</a>
</g>

  <image id="animal" width="80" height="80" xlink:href="https://media.giphy.com/media/182TlvsqrfvlH5dnFm/giphy.gif" x="25" y="550">

  
  <animate 
           xlink:href="#animal"
           attributeName="x" 
           from="600"
           to="1100" 
           dur="6s"
           begin="0s"
           repeatCount="indefinite"
           fill="freeze" 
           id="rect-anim"/>

</svg>
</figure>

The code essentially mirrors this example: http://jsfiddle.net/wgh52j39/

Answer №1

A few possibilities:

  1. To enable uploading any SVG as an image via the media uploader, you can insert the following code into your functions.php file or a plugin:
/**
 1. Allow SVG files to be uploaded via Media Uploader
 */
    add_filter( 'upload_mimes', function( $mime_types ) {
        $mime_types['svg'] = 'image/svg+xml';
        return $mime_types;
    }, 1, 1 );

    add_filter( 'wp_check_filetype_and_ext', function( $mime, $file, $filename, $mimes ) {
        $wp_filetype = wp_check_filetype( $filename, $mimes );
        if ( in_array( $wp_filetype['ext'], [ 'svg' ] ) ) {
            $mime['ext']  = true;
            $mime['type'] = true;
        }
        return $mime;
    }, 10, 4 );

It's important to note that allowing SVG uploads may pose security risks if users are not aware of potential malicious code within SVG files.

  1. In my experience with the Gutenberg editor, pasting SVG code into a custom HTML block works for me. Results may vary with the classic editor.

  2. Alternatively, manually upload the SVG file via FTP/SFTP/SSH to your server and link it using:

<img src="https://yourdomain/the/folder/you/used/filename.svg">
  1. To cater to small icon SVGs, you can encode the SVG as CSS background images by utilizing tools like URL Encoder:
.icon-heart { display: inline-block; width: 40px; height: 40px; background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20fill%3D%22%23dd0000%22%20d%3D%22M21.3%2C3.3c-2.5-2.1-6.1-1.7-8.4%2C0.6L12%2C4.8l-0.9-0.9C8.9%2C1.6%2C5.2%2C1.2%2C2.7%2C3.3c-2.8%2C2.4-3%2C6.7-0.4%2C9.4l8.7%2C9%20c0.6%2C0.6%2C1.5%2C0.6%2C2%2C0l8.7-9C24.3%2C10%2C24.1%2C5.7%2C21.3%2C3.3L21.3%2C3.3z%22%2F%3E%3C%2Fsvg%3E); }
<span class="icon-heart"></span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with Jquery Sticky Navigation

I've been struggling to understand this issue and can't seem to find any help. http://fiddle.jshell.net/DQgkE/7/show/ The user experience is currently a bit glitchy, but what I would like is: 1) When scrolling down the page, I want the Sticky ...

Ensure that flex items are evenly distributed when they wrap around in the layout

I am facing a challenge with my flexbox container where I want it to display elements in multiple rows when there are more than a set number of items. Below is my current approach: .container { display: flex; width: 400px; background: black; pad ...

Unable to back out of side navigation

My side navigation menu is giving me some trouble. It opens up just fine when I click the button, but it won't close back up when I click it again. The button is supposed to toggle between opening and closing the side navigation. I've looked at ...

An impressive animation feature when presenting

I'm working on a code where I need to implement a small animation in JavaScript for displaying an element with a delay after a certain function. Here's what I have so far: function OpenPanelEdit() { const element = document.getElementsByClass ...

Using jQuery to trigger a Bootstrap modal when a button is clicked

I've been attempting a simple task, but it appears to be unsuccessful. I'm trying to handle the click event of a button inside a bootstrap modal, and so far, nothing happens when clicking the button. My guess is that my jQuery selector for select ...

Updates made to CSS are not appearing on the Joomla site

My website is based on Joomla and has been functioning well since it was created. However, recently I have noticed that any CSS changes made in the files are not showing up on the site. I have tried clearing the cache and viewing it from different ISPs, ...

Passing parameters to a function triggered by a DOM event

I am struggling to pass an argument to a function during a JavaScript event. When the drop-down menu is focused, the text changes based on the argument passed to the function call. I can't seem to get this functionality to work! Any assistance would ...

How come the fa-heart-o icon is not functioning properly in Fontawesome version 5?

How can I use the fa-heart-o icon in Fontawesome 5 free version if it's not working? Can I have both version 4.7 and 5.1 of Fontawesome on the same page? It seems to work in version 4.7. This is the code I'm using: <!DOCTYPE html> &l ...

How can I use JavaScript to alter the background color of a dropdown option when selecting a different option?

Below is the code snippet for a dropdown menu: <select name="sourcesSelect" id="{{this.commonID}}" class="custom-select sources" data-color="" placeholder="{{this.status}}"> <option value=&q ...

Placing a div within a 960 grid system can sometimes result in unexpected spacing

Testing on various browsers: Chrome, IE, Firefox. I'm facing an issue where the BusinessNav ID keeps getting pushed over 3 columns. Desired page layout can be found here I could definitely hack this code to adjust the positioning. However, with so m ...

Disappearance of CSS background images in Chrome

This specific issue seems to be isolated to Google Chrome running on Mac OS X (Chrome 17). I have conducted tests on all the major browsers on both Mac and Windows 7. The problematic page can be found here: The page utilizes JQuery AJAX to load divs. On ...

Guide to crafting a flawless pixel-perfect separator with CSS borders

I have a menu with a black background and a submenu with a grey background. Now, I want to add a pixel-perfect divider between them. However, I'm unsure of which border color to use in order to achieve this perfection. Can anyone provide some guidan ...

Is it possible to create a grid by arranging each statement with a specific format?

After fetching and parsing some data, I have utilized the 'each' function to display it: $.ajax({ url : 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent('htt ...

Implement drop-shadow filtering specifically for Internet Explorer

I am currently implementing a drop-shadow filter on a curved png, and while it works perfectly in most browsers, IE seems to be the exception. You can see the issue with the boxes on this page: (Three boxes located just below 'Yoga Sequence Builder ...

Struggling to add a border to an HTML div element

I'm completely baffled as to why I can't seem to add a border around my div. Here is the link to my jsfiddle: http://jsfiddle.net/4HnKs/1/ It's possible that I've been staring at my computer screen for too long, but no matter how hard ...

Styling the text for ARBONNE with CSS

Currently, I am working on a website for a friend and he has requested that the text font be AR BONNIE. Can anyone provide me with the CSS code needed to implement this font? I have searched everywhere but cannot seem to find it. ...

Tips for aligning buttons in a row

I am trying to find a way to make my buttons more compact within a <div> element when the length exceeds 100%. Is there a way to achieve this while avoiding wrapping to a second line? Is there a solution similar to using scaleX, but one that takes i ...

Interact with elements across multiple SVG files using HTML

I have a website that consists of three SVGs. I am now looking to connect elements in these different SVGs by drawing simple lines between them. My goal is to create a line from element1 (#element1) in svg1 to element6 in svg2. Unfortunately, I am unable ...

What is the best way to dim the main content/canvas of a website when the side menu is displayed

Currently, I am utilizing Bootstrap 3 with the Jasny Off Canvas Reveal Menu template. You can view it here: Everything is functioning well, but I have a specific goal in mind. I want to achieve a similar effect as seen on this site when clicking the hambu ...

What is preventing these arrows from spinning?

I am struggling to find a solution to rotate the arrows on the x-axis, which are added by the::before pseudo-element when the link is active. Despite trying keyframes and searching online, I have not been able to achieve the desired result. Below you wil ...