Switching From HTML to Visual Editor and Back Completely Strips Page Contents

I’ve had this code in a custom local plugin for a while. Or you could just stick it in your theme’s functions.php:

// Allow iframe in TinyMCE
function tinymce_add_iframe( $arr = array() ) {
  $extra="iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]";

  if ( isset( $arr['extended_valid_elements'] ) ) {
    // append to existing value
    $arr['extended_valid_elements'] .= ',' . $extra;
  } else {
    // set the value
    $arr['extended_valid_elements'] = $extra;
  }

  return $arr;
}

add_filter('tiny_mce_before_init','tinymce_add_iframe');

This tells TinyMCE (the visual editor) to allow the iframe tag and all of its attributes.