Support

Account

Home Forums Feature Requests File field type: Option to return SVG as code

Solved

File field type: Option to return SVG as code

    • arleym

    • January 10, 2016 at 1:57 pm

    Currently the File Type can return as a File Array, File URL, or File ID. What would really be great would be returning SVG as the actual code (putting SVG into templates as code make them styleable with CSS).

    I think returning code would only make sense for SVG.

    • arleym

    • January 10, 2016 at 2:24 pm

    Ha! After a bit more research I see that I can use PHP to get the SVG contents after all,

    <?php $logo = get_field(‘header_logo’, ‘option’); ?>
    <?php echo file_get_contents( $logo ); ?>

    Problem solved! I hope this is helpful for someone else.

  • I was looking for this exact answer! Thanks! However, I need to output the svg for each term in a taxonomy loop. When it comes to that, ACF requires the term variable to be passed on as a second parameter in get_field().

    So I tried this:

    <?php $svgImg = get_field('my_custom_field', $term, 'option'); ?>
    <?php echo file_get_contents( $svgImg ); ?>

    But I get this php error:

    “Warning: “file_get_contents() expects parameter 1 to be a valid path”

    Seems like it´s not an issue related to the order of the parameters as when I swap $term and option, it makes WP blind to what term I´m referring to:

    “Warning: file_get_contents(): Filename cannot be empty”

    $term has been properly defined as I have tested it with:

    <?php $svgImg = get_field('my_custom_field', $term); ?>
    <img src="<?php echo $svgImg['url']? />">

    This last piece of code works. But the previous doesn´t.

    Can somebody help?

  • God! It´s a bit embarrassing when you get the answer seconds after posting the question. But instead of removing it, I´ll just leave it here as it might help someone else, just like arleym´s post helped me.

    Arleym was using a file field. I am using an image field. And I need it to be like that for reasons that go beyond this topic. (obs: wordpress does not support svg in the media library natively).

    Having said that, it wasn´t working because my image field was set to return the object array while it MUST be set to return the URL, instead.

    So the intial code on my previous comment was correct and after changing the value returned by the image field it now works!

    <?php $svgImg = get_field('my_custom_field', $term, 'option'); ?>
    <?php echo file_get_contents( $svgImg ); ?>

    Hope this may help somebody else.

  • I just want to say thank you to both @diogo_wagner and @arleym. This is exactly what I needed.

    • dhandi

    • August 28, 2020 at 6:10 am

    Three years later and this post is still a lifesaver. Thanks for figuring this out guys!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.