[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.

シェア!

この記事を書いた人

kenichiのアバター kenichi エンジニア・写真家 | Engineer and photographer

Nomadic worker who travels all over Japan and abroad; worked as a technical sales person for five years before going independent.
Works as a freelance engineer on website production and application development. Currently working to bring interesting things by interesting people to the world, while seeking to move abroad.

目次