Skip to content
Advertisement

Can I pass wp permalinks to an svg file?

I’ve got an svg file for a hero image, and I want certain paths to be internal links. I’ve wrapped the paths in an anchor tag, and putting in static links works, but I’d like them to be dynamic so I can changed them in the wp admin. Is this possible? How can I send the permalink data to the svg file?

The bit of code below shows what I’ve got right now in my svg file…

JavaScript

…but ideally it would look more like this :

JavaScript

Advertisement

Answer

You can code your SVG file giving your hyperlinks id values, for example:

JavaScript

You can load them into your web page with HTML like this. When you use <object> to load svg files, they are made accessible to Javascript in your page as if they were ordinary HTML. (Not so with <img>.)

JavaScript

Write a bit of php to deliver the hyperlink value to your web page.

JavaScript

Then you can write some Javascript to alter the hyperlink.

JavaScript

This kind of thing is quite tricky to debug. (I didn’t debug it.) But it is the way you customize the contents of SVG files.

The good news is, once you get past all the fiddly event programming, it is robust, now that Internet Explorer is in the dustbin of history where it always belonged.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement