Skip to main content

Wordpress iframe function

/**
 * Allow <iframe> tags in post *and* page content.
 */
function my_allow_iframes_in_content( $allowed_tags, $context ) {
    // WP calls wp_kses_post() on both posts and pages, passing 'post' as the context.
    // But if you want to be explicit you can also check for 'page':
    if ( in_array( $context, [ 'post', 'page' ], true ) ) {
        $allowed_tags['iframe'] = [
            'src'             => true,
            'width'           => true,
            'height'          => true,
            'frameborder'     => true,
            'allowfullscreen' => true,
            'loading'         => true,
            'style'           => true,
            'class'           => true,
            'id'              => true,
            'name'            => true,
            'sandbox'         => true,
            'allow'           => true,
            'referrerpolicy'  => true,
        ];
    }
    return $allowed_tags;
}
add_filter( 'wp_kses_allowed_html', 'my_allow_iframes_in_content', 10, 2 );