I'm trying to embed an iFrame, and because I set up some CSS using responsive design, I wish to NOT have width="" and height="" attributes whatsoever.

When I type this into the HTML editor field..

<iframe src="https://docs.google.com/document/pub?id=1fsm-cvpdfX-8tXauGrQgRmgWONTpX8J3almvZhzBeCA&amp;embedded=true" class="scale-with-grid"></iframe>

And then I switch to visual mode and come back, it immediately removes the class="scale-with-grid" and changes it to width="320" height="240"

<iframe src="https://docs.google.com/document/pub?id=1fsm-cvpdfX-8tXauGrQgRmgWONTpX8J3almvZhzBeCA&amp;embedded=true" width="320" height="240"></iframe>

How do I avoid this? I used this "Raw HTML" plugin but it didn't apply to this iframe situation whatsoever.


2 Answers 2


On default is the iframe tag a not allowed tag in the editor. You must change the filter for allowed this html tag. Copy the follow source in a plugin and allow iframe for the editor TinyMCE.

add_filter( 'tiny_mce_before_init', 'fb_change_mce_options' );
function fb_change_mce_options( $tags ) {

    // Comma separated string od extendes tags
    // Command separated string of extended elements
    $ext = 'iframe[align|longdesc|name|width|height|frameborder|scrolling|marginheight|marginwidth|src|id|class|title|style]';

    if ( isset( $tags['extended_valid_elements'] ) )
        $tags['extended_valid_elements'] .= ',' . $ext;
        $tags['extended_valid_elements'] = $ext;

    return $tags;
  • I think we need class attribute more.
    – Anh Tran
    Sep 4, 2012 at 9:53
  • Have you actually tried this in latest WordPress, and was it successful? I can't get this to work with iframes specifically, after adding both class attribute and setting paste_strip_class_attributes to none. it seems iframe is a special case.
    – Milo
    Sep 4, 2012 at 13:57
  • @Milo: yes, you have right. I have tested now and the class attribute was always filtered. But I have not find an solution in the small time frame, where I tested this.
    – bueltge
    Sep 5, 2012 at 10:07
  • This problem for class attribute is an TinyMCE problem and I dont find an solution; only an custom init in oninit param was possible, but this is a great overhead for this requirement.
    – bueltge
    Sep 5, 2012 at 11:59
  • 1
    I think the solution for allow class on iframes is the embed plugin; maybe little bid to much, id is possible, bute usable; see the embed plugin
    – bueltge
    Sep 5, 2012 at 12:44

You could make use of an iframe-shortcode. Here's a small plugin to allow that:

! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: (#62729) Shortcode to display an iframe */

function wpse62729_allow_iframes_tinymce( $attr )
    global $post;

    $attr = shortcode_atts( array(
         'src'          => ''
        ,'width'        => '100%'
        ,'height'       => '100%'
        ,'name'         => md5( $post->post_title )
        ,'scrolling'    => 'no'
        ,'marginheight' => 0
        ,'marginwidth'  => 0
        ,'frameborder'  => 0
        ,'align'        => is_rtl() ? 'right' : 'left'
    ), $attr );

    $html = "<iframe";
    foreach ( $attr as $key => $val )
        $html .= " {$key}='{$val}'";
    $html .= "><p>";
    // Message if not possible to display iframe
    $html .= sprinft( 
        'Displaying the iframe is not possible. View the %ssource following this link%s.'
        ,"<a href='{$attr['src']}' target='_blank'>"
    $html .= "</p></iframe>";

    return $html;
add_shortcode( 'iframe', 'wpse62729_allow_iframes_tinymce' );
  • why an shortcode for an default tag, its more to parse the content and stopper for performance? The TinyMCE can iframe, only the class attribute is an problem in the current version, but the id attribute works and is a chance for custom style.
    – bueltge
    Sep 9, 2012 at 15:26
  • @bueltge It's just an alternative approach to get around missing (some) support for TinyMCE quickly.
    – kaiser
    Sep 9, 2012 at 15:29
  • Yes, this is. But I see many problems of long exists webspace, there use shortcodes for a while and not supportet now.
    – bueltge
    Sep 9, 2012 at 15:32
  • @bueltge I'm not sure, if I understood you correctly (just explain it again in german - you can delete it later), but at least it's a plugin.
    – kaiser
    Sep 9, 2012 at 15:36
  • 1
    This works perfectly fine! Found a plug-in myself actually, had to make minor mods. web-profile.com.ua/wordpress/plugins/iframe
    – jeffkee
    Sep 11, 2012 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.