Support

Account

Home Forums General Issues SVG code getting stripped out of field

Solving

SVG code getting stripped out of field

  • This is happening on a site after updating ACF. I’m not sure what version introduced this bug/feature but it’s a problem.

    I have meta fields setup on menu items. One is a texterea set to return with no formatting. This field is intended to hold SVG code (an icon for the menu item). It previously saved just fine. Now, when saving, it wipes out all of the SVG code on all items. I suspect there’s some sort of sanitizing that has been added? It saves other text in the field just fine. It’s SVG code, or javascript (not that I’m using that in the field – just checked for testing purposes) that’s getting stripped out on save.

    How can I prevent this particular field from sanitizing? Assuming that’s the issue.

  • I suspect it was version 6.0.4 that introduced this sanitization based on the change log: https://www.advancedcustomfields.com/blog/acf-6-0-4/

  • Seems like this might be limited to textareas on nav menu items. I can save <svg> code just fine if I add it to a textarea in other locations, like on a Post.

  • I’ve been in touch with support via email and shared a video of the issue. Sounds like it’s a bug – they were able to replicate it. Hopefully will be addressed quickly.

  • I ran into the same issue with ACF text area fields used to hold SVG code for menu icons. Each time I would enter the SVG code, it would not save. From the link posted by John above, I came up with the following (added to functions.php) which resolved my issue. I still get security notices from Wordfence – but the SVG code can now be saved.

    /**
     * ACF SVG filter to allow raw SVG code.
     * 
     * https://www.advancedcustomfields.com/resources/html-escaping/
     * 
     */
    add_filter( 'wp_kses_allowed_html', 'acf_add_allowed_svg_tag', 10, 2 );
    
    function acf_add_allowed_svg_tag( $tags, $context ) {
        if ( $context === 'acf' ) {
            $tags['svg']  = array(
                'xmlns'				=> true,
    			'width'			=> true,
    			'height'		=> true,
    			'preserveAspectRatio'	=> true,
                'fill'				=> true,
                'viewbox'				=> true,
                'role'				=> true,
                'aria-hidden'			=> true,
                'focusable'				=> true,
            );
            $tags['path'] = array(
                'd'    => true,
                'fill' => true,
            );
        }
    
        return $tags;
    }
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.