Enable rendering of SCRIPT tag without HTML escaping
-
My employer switched from a decades old custom code base to WordPress recently. One of the benefits he was looking forward to was being able to drop any video embed code in a WYSIWYG field wherever needed. This includes IFRAMES, OEMBED URLS, and JAVASCRIPT. We couldn’t get this to work dynamically via ACF fields in a template until I stumbled across the wp_kses_allowed_html() function. I was able to get iframes working with this code:
add_filter( 'wp_kses_allowed_html', 'acf_add_allowed_iframe_tag', 10, 2 ); function acf_add_allowed_iframe_tag( $tags, $context ) { if ( $context === 'post' ) { $tags['iframe'] = array( 'src' => true, 'height' => true, 'width' => true, 'frameborder' => true, 'allowfullscreen' => true, ); } return $tags; }
So then I tried to build something similar to enable SCRIPT tags — I know they’re disabled to prevent XSS attacks, but my employer feels enabling them is worth the risk because we can just restore a backup.
This is the my code to enable SCRIPT / JavaScript rendering — why isn’t this working?
add_filter('wp_kses_allowed_html', 'acf_add_allowed_script_tag', 10, 2); function acf_add_allowed_script_tag($tags, $context) { if ($context === 'post') { $tags['script'] = array( 'async' => true, 'crossorigin' => true, 'defer' => true, 'integrity' => true, 'nomodule' => true, 'referrerpolicy' => true, 'src' => true, 'type' => true, ); } return $tags; }
The page I need help with: [log in to see the link]
- The topic ‘Enable rendering of SCRIPT tag without HTML escaping’ is closed to new replies.