Allow Image HTML tags in WordPress Comments

By hooking into the wp_kses_allowed_html filter, you have full control over what HTML is allowed to be displayed in post and comment content.

Unfortunately the $context provided by the filter’s 2nd parameter does not describe the difference between a post and a comment, so we have to alter them all equally.

<?php
add_filter( 'wp_kses_allowed_html', array( $this, 'my_kses_allowed_html_hook' ), 20, 2 );
function my_kses_allowed_html_hook( $tags, $context = null ){
	if ( 'post' == $context && ! isset( $tags['img'] ) ) {
		$tags['img'] = array(
			'src' => 1,
			'height' => 1,
			'width' => 1,
			'alt' => 1,
			'title' => 1
		);
	}
	
	return $tags;
}

If you have any thoughts on how to improve this snippet, let me know below!

0 Thoughts

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *