How to Embed iFrames in WordPress Posts

WordPress can do a lot to streamline your workflow, but every now and then it’ll throw a little roadblock in your path. For instance, if you’re trying to embed a video from YouTube or add Amazon’s Affiliate Marketing advertisements to your site, both of which use iFrames, you will soon find that they mysteriously disappear every time you switch from HTML to Visual mode in the editor. Needless to say, this can be very frustrating.

Modifying WordPress to allow iFrames

An iFrame is a snippet of code that allows you to embed one website inside another website. Lots of sites (Google AdSense, Amazon, YouTube, Facebook, etc.) use this to share content, and it can be tremendously useful. On the other hand, it can also be very dangerous. Since you are embedding a third-party site, you have absolutely no control over the content of that site. If that site puts up something dangerous, it’s going to display on your website. You should only use iFrame for website you trust completely.

While you can work around this issue using a plugin, there’s a simpler option available to you. Simply place the below code in your “functions.php” file before the ?> at the end of the file. You can edit this file by browsing to Appearance > Editor, and then selecting “functions.php” from the list of templates on the right. Alternatively, you could edit functions.php directly in your theme’s directory.

//Allow iFrames
add_filter('tiny_mce_before_init', create_function( '$a',
'$a["extended_valid_elements"] = "iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]"; return $a;') );

That’s it! You can now embed iFrames into your WordPress posts. Just be sure to only embed them from sites you trust.

27 Responses to “How to Embed iFrames in WordPress Posts”

    • fastharryDOTcom

      You are welcome David….A quick question…When I google the Iframes issue, I find all types of code to add to the funcions PHP file. Just curios why that is. Just different ways to achieve the same thing?…I will also tell you that yours was the only article to mention where to put the code in the file…ya know, at the end before the question mark. That type of stuff is important for non coders…Thanks again..Harry

    • David

      Yep, different ways of accomplishing the same task. As I mentioned, there are also plugins out there to allow iFrames as well.

      The “where to place” this issue is something that always bugs me in articles like this. Technically, you can place the code anywhere between the opening and closing php tags, however, you don’t want to accidentally place it into another function. Putting it at the end of the file, before the closing, just makes it easier for everyone.

  1. fastharryDOTcom

    thanks david…If you are allowed to..can you point me to a plug in…I looked at everything related to IFRAMES…but none seemed to fit the bill…all had something to do with wimeo or whatever….Just curious is all..Thanks..BTW, is this your site?

  2. fastharryDOTcom

    Hi David…

    Thanks again…and I have to tell you I am most impressed with your site and the way you answered my questions. I will absolutely keep you in mind for any web stuff in the future.

    Where are you located?…I am in NJ and CA…

    • David

      I very much appreciate that!

      I’m located in Michigan; metro Detroit specifically.

  3. fastharryDOTcom

    Cool, just wanted to see if you are USA located…Can I make one suggestion?..If Possible, maybe you can add a “notify if anyone reply’s check box” for the forums….This way people would get notification that someone replied…It might lead to more banter and keep people returning to the site instead of forgetting that they posted or forgetting where..

    • David

      That’s an excellent suggestion. I’ll start looking for a good way of doing that.

    • David

      I added a comment subscription system. Looks like it works pretty well, but please let me know if you run into any issues.

  4. fastharryDOTcom

    thanks…I have your site bookmarked…If I can ever help you or you want to run something past me I would be glad to help. Always like making new friends and contacts on the net….BTW, I am in the Google + beta and have invites if you would like one…email is support at

    • David

      Much appreciated, FastHarry. I’m still holding off on Google+ until businesses are officially supported. It seems like a really nice solution though.

  5. fastharry

    No problem…send me an email to the support email I left so I can send you a + invite and this way you have it whenever you are ready. ..Though I would make a profile and keep it bare minimum…this way you are on there and get all the first info, esp when they release the business page info…

    BTW, Nice job on the reply email thingie..

    • David

      Hey thanks FastHarry. I’ve got an invite now; just biding my time until business pages come out (it’s got to be soon, right?).

  6. fastharry

    I think it will be….Let me know when you get on..I can add you and follow Red Star…If I hear anything on my end since I am on I will let you know so you can join up..

  7. Tomaszsu

    Thanks. I was googling for this solution at least for 40 minutes, before I arrived to your website. Great suggestion.

  8. james

    this did nothing for my page. I’m trying to add the following ifram to my website using the LocalBiz 1.0 theme:

    Seems simple enough, right? Nothing but a big blank space. Added your code to my funtions.php file, no change.

    • David

      What usually happens is that WordPress deletes the iFrame. So if you didn’t have that code in functions.php before you added the iframe, I’d try adding it in again.

  9. Jim

    I just wanted to say thank you! This was the solution I was after and once the nerves about messing with something I know nothing about settled I applied your code. Perfect 🙂

    • David

      Remove the code and re-upload to clear it out. If you’re having problems, I’d suggest finding a plugin.

Leave a Reply

  • (will not be published)

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