• Resolved Ben1098765

    (@ben1098765)


    Hi there

    Fantastic plugin, but I’m having issues when outputting an uploaded SVG file. I use the following code to render it as an inline SVG:

    include get_attached_file($file_ID);

    The error I get is:

    Parse error: syntax error, unexpected identifier “version” in?/<filepath and name>.svg?on line?1

    The issue seems to be the XML version tag, which gets added when I upload the SVG.

    <?xml version="1.0" encoding="UTF-8"?>

    When I remove that line from the file in the /uploads/ folder, the error goes away and the image is rendered correctly.

    This happens with PHP 7.3, 8.1 and 8.2, and also WordPress 6.2.

    It’s not there when I upload it; the plugin adds it. And I need to include it as an inline SVG so that it can be styled dynamically with CSS.

    Any way around this? Can I stop the xml tag being added somehow? Or is there a better way for me to render the file on the PHP page? Manually editing an uploaded file via FTP isn’t really feasible for me to ask a client to do.

    Thanks in advance.

    Ben

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Darin Kotter

    (@dkotter)

    @ben1098765 Thanks for the question! Can you provide more information on what you’re trying to achieve? I’ve not personally seen a use case where an SVG file is included directly like this, typically the SVG would be loaded within an img tag, so just trying to understand your use case. Thanks!

    Plugin Support Darin Kotter

    (@dkotter)

    As a follow-up, if you want just the raw SVG output without wrapping it in an img tag, I think you can do echo file_get_contents( get_attached_file( $file_id ) ) instead of the include approach

    Plugin Contributor Daryll Doyle

    (@enshrined)

    @ben1098765 in addition to the comments from @dkotter above. I also wanted to mention that I’d also suggest using file_get_contents() rather than include().

    I write more about this a few years ago at https://enshrined.co.uk/2018/09/19/how-to-properly-include-inline-svgs-in-a-wordpress-theme/ if you want more context.

    Thread Starter Ben1098765

    (@ben1098765)

    Hi both

    Thanks for the responses – echo file_get_contents( get_attached_file( $file_id ) ) fixed the problem perfectly. I don’t know why include worked when I manually removed the XML version tag from the uploaded file, but that’s a query for another day (probably something along the lines of <?xml being interpreted as PHP at a guess).

    It’s great to know that get_file_contents(get_attached_file is a more robust approach anyway; thanks for the article Daryll.

    As for the use case, I have the client being able to upload SVGs, which then get certain styles on interaction using CSS. That obviously can’t be done with an img, only inlined. And they’re being loaded from the media library, not the theme. These are project specific, but a more general example would be uploading SVG social icons to the media library, which you’d typically want to get a colour change on rollover/focus.

    Anyway, thanks again – great support. Nice to be able to draw a line under it! ??

    Ben

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘XML version tag causing errors on output’ is closed to new replies.