Allow Iframes In WordPress Posts

If you’ve ever tried adding an iframe to your WordPress post, and then switched the TinyMCE editor to Visual mode, you’ve found that the editor strips out your iframe code. This can be annoying. Just add this snippet to the functions.php file in your theme folder to prevent TinyMCE from killing your iframe.

function add_iframe($initArray) {
$initArray['extended_valid_elements'] = "iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]";
return $initArray;
}
add_filter('tiny_mce_before_init', 'add_iframe');

9 Comments
  1. Jonathan October 22, 20106:30 am

    So I probably shouldn’t be in messing with files when I don’t know what all of them do and what they’re for, but if not for the help from an Indian programmer friend, my site might have been down for a while.

    Completely my fault for not understanding before playing around, but for future people using this code, you might want to tell them “which” functions.php file this code goes into and how to properly insert it…

    I had a moment of panic when my site crashed after I added this code to the functions.php file in my theme, and then tried instead adding it to the functions.php file in my wp-includes folder, which also took down my site.

    (Backups are always a good idea… glad I’ve at least learned that at this point.)

    Please let me know where I should put this code and if there’s anything I need to surround it with to make it work. I think others would probably find that helpful.

    If you can let me know where to put it, I will make a YouTube video of me adding it to my site and link back to you to sing your praises. (Well, maybe not sing, but at least talk nicely).

    If you can pretend you’re teaching PHP to a newborn (which I’m not much above), that would be very helpful.

    Thanks very much!

  2. Chad Von Lind October 22, 20107:40 am

    Haha Oh no! Glad you got the site up quickly. This code belongs in the functions.php file within your theme. And as a rule, you should never mess with files outside of wp-content. They would be overwritten during an upgrade anyhow.

    Also the code should be in between some opening and closing PHP tags as well.

  3. alotofpink January 22, 20112:10 pm

    I love this!
    it works like a charm…
    and I did that mistake to put it to the main functions php… don’t do that :D will end up like me with a messed up wordpress after upgrade

  4. web-developer May 12, 20119:47 am

    WordPress removes iframe when you switch from “HTML” to “Visual” tab in your editor because of the security reasons.
    You can use this plugin to embed iframe into post content using such shortcode: [iframe width="640" height="480" src="http://player.vimeo.com/video/3261363"]

  5. dij June 24, 201112:28 pm

    OMG, I have been looking for something like this for MONTHS!!!!! Thank you so much!

  6. dij June 24, 201112:38 pm

    btw, don’t use a plug in. You have to constantly upgrade them and then they’re no longer supported. Use this fix. It makes life SO MUCH EASIER!

    Again, thank you so much. This comes from someone who has a huge movie/entertainment site that uses a lot of videos that this is a life saver!

    Again make sure to paste it into your functions.php file that is being used with your theme. Make sure to put it just above your PHP closing tag that looks like this:

    ?>

    Thanks again!!!

  7. web-developer July 1, 201111:38 am

    To dij: what will happen if you will change your theme?
    It is better to put this code into a new plugin and activate it.

  8. Chad Von Lind July 1, 201111:54 am

    @web-developer – A good question. As a rule, I keep a list of default functions saved, and for every theme I build or use, I just add them to the functions file. This is one function that I include in my default list.

Submit Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

To include your code, use pastebin, and paste the URL in the comment body. Failure to do so may prevent your comment from appearing. Thank you.