Support

Account

Home Forums General Issues SVG code getting stripped out of field Reply To: SVG code getting stripped out of field

  • 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;
    }