Add HTML tags to the allowed tags list in WordPress

May 21, 2015

By default WordPress limits what HTML tags are allowed in post content. If tags that are not whitelisted are found in the content they will be stripped. It is possible add additional tags to the allowed list.

View allowed tags

To view the list of currently allowed tags you can use the following code:

[php]
add_action( ‘init’, function() {
echo allowed_tags();
});
[/php]

Add to the allowed tags

In this instance I need to allow the iframe tag. Be sure to also specify the allowed attributes:

[php]
add_action( ‘init’, function () {
global $allowedtags;

$allowedtags[‘iframe’] = array(
‘src’ => array(),
‘height’ => array(),
‘width’ => array(),
);
});
[/php]

Add allowed tags to the WP Editor

Using the above will allow the iframe tag in code, but what if you want to allow the iframe to be added using the post editor? There is another filter used by tinyMCE which also needs to be set:

[php]
add_filter(‘tiny_mce_before_init’, function( $a ) {
$a["extended_valid_elements"] = ‘iframe[src|height|width]’;
return $a
});
[/php]