[WordPress] Enabling iframes for posts retrieved with shortcodes.

I had defined a shortcode in function.php and retrieved the articles in it. I solved this problem because I could not display the article properly if there was an iframe in the article.

WordPress provides the ‘wp_kses_allowed_html’ function, so I used this.

The ‘wp_kses_allowed_html’ allows HTML tags to be allowed by giving them to the context.

Simply define the tag ‘iframe’ that you want to allow and its attributes that you want to allow in an array.

function custom_wpkses_post_tags( $tags, $context ) {
	if ( 'post' === $context ) {
		$tags['iframe'] = array(
			'src'             => true,
			'height'          => true,
			'width'           => true,
			'frameborder'     => true,
			'allowfullscreen' => true,
		);
	}

	return $tags;
}

add_filter( 'wp_kses_allowed_html', 'custom_wpkses_post_tags', 10, 2 );

The implementation is simple. This content is based on an issue on GitHub. Thanks to our predecessors.

よかったらシェアしてね!
目次