How to change the allowed HTML tags for WordPress

WordPress by default filters out potentially harmful and/or invalid HTML. But in some cases you might want some of that invalid HTML!

One of those cases occurred today when we needed to embed a Google Form in a post. To do so, Google provides you with HTML code that contains a iframe, but uh-oh! The iframe element is not allowed by default.

If you look in wp-includes/kses.php you see a variable called $allowedposttags. The array contains all the allowed tags, and their allowed attributes. So you could also change the allowed attributes for the already allowed elements if you so wish.

To include iframe in the list of allowed element I added the following code to my theme’s functions.php:

$allowedposttags["iframe"] = array(
 "src" => array(),
 "height" => array(),
 "width" => array()
);

And something that is always requested is the ability to simply just paste the embed-code provided by YouTube or Vimeo without having it filtered out. To do so simply add this to your functions.php or plugin:

$allowedposttags["object"] = array(
 "height" => array(),
 "width" => array()
);

$allowedposttags["param"] = array(
 "name" => array(),
 "value" => array()
);

$allowedposttags["embed"] = array(
 "src" => array(),
 "type" => array(),
 "allowfullscreen" => array(),
 "allowscriptaccess" => array(),
 "height" => array(),
 "width" => array()
);

But if you want to embed Flash files in a more standard compliant and modern way, check out our Easy Flash Embed plugin instead!

About

Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: , , ,
Posted in Intermediate
26 comments on “How to change the allowed HTML tags for WordPress
  1. […] How to change the allowed HTML tags for WordPress March 2010 […]

  2. Cathy says:

    Thanks for the tip! I needed to allow id attributes on my div tags to stop the layout breaking and your solution was just the job!

  3. Kathy says:

    just what i needed!

  4. […] amigos en la red que me apuntaron en la dirección correcta: Fuente: HTML5 Tag Reference Fuente: WordPress Quick Tips Publicado en Diario, HTML, Videos | Etiquetado diario, html, html5, videos | Deja una […]

  5. Rifal says:

    I cant understand, i cant followed this tips, very difficult for me.. :lol

  6. John D says:

    But this won’t work in WordPress Hosted Website. So what is the solution?

    you can’t change functions.php on a site at wordpress.com.

  7. abogado says:

    Okay, sorry for being so new, do we are referring to wordpress.com or wordpress.org? I work with. com and I can not (or is that I lack knowledge) to do that.

  8. Viral says:

    is it possible to have specific values for attributes? For e.g I want to allow my users to use the script tag but the src attribute should always.. start with https://www.abcd.com

  9. Denver says:

    Its look like little tricky to the newbie like me.

  10. Fiona D says:

    Great help, thank you. If you just want to add an attribute to a tag and keep the rest of the attributes defined in the WP core files, eg add “id” to the div tag, the following code adds a single attribute.

    $allowedposttags[“div”][“id”] = true;

  11. edrahil says:

    Simple and perfect ! It works for my iframes, more easy than adding a plugin.
    Thanks

  12. Simple and perfect ! It works for my iframes

  13. Marketiva says:

    may be we can use some plugins out there?

  14. Hugh says:

    Good article post, thanks.

  15. muhtarul says:

    A useful post, it give me more knowledge. thank you

  16. Rose says:

    It’s really a cool and helpful piece of information. I’m glad that you just shared this helpful info with us. Please stay us up to date like this. Thanks for sharing.

  17. […] like you can change the allowed tags: How to change the allowed HTML tags for WordPress | WordPress Quick Tips Hope this is still relevant to the current version. __________________ [3G] UK: giffgaff – 250 […]

  18. Mario Ohoven says:

    Thanks for sharing. Plugins often makes trouble. This way is really helpful.

  19. Mike Pann says:

    Hey Vincent – I think WordPress allows iFrame if you paste the code in text mode. Can you please consider adding an update to this post if possible?

    Anyways I enjoy your other posts you have written.

    Thanks
    Mike
    HTML5 Training

Leave a comment

About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 121 other subscribers