WordPress iframe Not Working or Disappearing? How to Fix It

Short answer
Why does my WordPress iframe code disappear or stop working?
First, be clear that "iframe problem" is the shared name for three very different problems: the code is deleted when you save, the code stays but the frame shows up blank, or the iframe works but looks wrong on screen. Telling them apart is most of the fix, and it saves you hours of searching in the wrong place.
If your WordPress iframe code keeps disappearing, the cause is almost always the same: the user account that saves the post does not have the "unfiltered_html" capability, so WordPress strips the iframe tag for security reasons. The fix is to paste the code into a "Custom HTML" block, use a trusted iframe plugin, or grant that capability through functions.php. When the iframe stays but shows nothing, the cause is usually the X-Frame-Options header of the site you are embedding.
An iframe (inline frame) is an HTML tag that lets you embed another page, video, map, form, or calendar inside a web page. In WordPress, this seemingly simple job often turns into a maddening problem: you paste the code, save, reload the page, and the iframe has vanished as if it never existed. Or the code stays, the frame is blank, and the console shows a "refused to connect" error. In this guide we go through every likely cause of WordPress iframe problems one by one, with code examples and step-by-step fixes for each. The goal is that you actually solve the problem, whatever scenario you are in.
The first group is the code disappearing completely from the editor. You paste the iframe tag and save, and when you come back the code is gone. This has to do with WordPress's own content sanitization, and it is almost always caused by a user permission or a security plugin.
The second group is the code staying in place while the iframe does not show. The iframe tag is in the HTML source, but in the browser you see an empty box, a white area, or a "this content cannot be displayed" message. This usually happens because the site you are trying to embed forbids other pages from framing it (an X-Frame-Options or CSP header).
The third group is visual and size problems: the iframe works, but it overflows on mobile, looks too small, shows a scrollbar, or breaks the page layout. This is purely a CSS matter. The table below helps you find the right group and cause quickly, based on the symptom you see.
| Symptom | Likely cause | Quick fix |
|---|---|---|
| The iframe code is deleted completely when you save | The user does not have the unfiltered_html capability | Custom HTML block + an Administrator account or an iframe plugin |
| The code is there but the frame is blank / "refused to connect" | The other site forbids embedding with X-Frame-Options / CSP | Use the other site's official embed code or allow the embedding |
| It disappears for some users only | Editor/Author role, no problem for Admin | Grant unfiltered_html to the role or use a plugin |
| "Mixed Content" warning in the console | HTTP iframe source on an HTTPS page | Change the iframe src address to https:// |
| It worked before an update, now it gets deleted | A newly installed security plugin is filtering it | Loosen the Wordfence/Sucuri HTML filter setting |
| The iframe works but overflows on mobile | Fixed width/height, no responsive CSS | Wrapper div + aspect-ratio CSS |
| The code survives in the classic editor but is deleted in a block | Wrong block type (you need Custom HTML, not Paragraph/Classic) | Add the block as "Custom HTML" |
Why does the block editor delete my iframe code?
This is the most common scenario, and understanding the logic behind it makes the fix permanent. For security reasons, WordPress "cleans" content when it saves it. At the center of that cleaning is a family of functions called wp_kses. wp_kses keeps a list of allowed HTML tags and deletes everything that is not on it. By default, the <iframe> tag is not on that safe list, because an iframe can in theory be abused to embed harmful content.
So why does it vanish for some users but not for the administrator? Because WordPress has a special capability called "unfiltered_html". Users who have it (on a single-site install, usually the Administrator and Editor roles) can save iframes, scripts, and other "unfiltered" HTML. When users without it (the Author and Contributor roles) save, the iframe is deleted. This matches how WordPress works: if a user lacks the unfiltered_html capability, their content passes through the wp_kses function, and they cannot add tags like iframe or embed to their posts.
On multisite (multi-site network) installs the rules are stricter. On a multisite network, nobody has the unfiltered_html capability by default, not even the Administrator; it belongs only to the Super Admin account. In the map_meta_cap() function, WordPress core ignores the unfiltered_html capability on multisite for users who are not Super Admins. So even a regular site administrator on multisite can lose the iframe code when adding one. This is designed security behavior, not a bug.
How do I add an iframe correctly with the Custom HTML block?
If your account has enough permissions (you are an Administrator or Editor on a single-site install), the cleanest way to add an iframe without a plugin is the Custom HTML block. The problem usually comes from pasting the iframe code into the wrong block type. If you paste raw HTML into a Paragraph block, the editor may treat it as text or corrupt it on save. The correct steps are:
1. In the post or page editor, go to the spot where you want the iframe to appear.
2. Click the plus (+) icon and type "Custom HTML" into the block search box.
3. Add the Custom HTML block.
4. Paste your iframe code into this block as it is. For example:
<iframe src="https://www.example.com/embedded-page"
width="600" height="400" loading="lazy"
title="Embedded content" frameborder="0"></iframe>
5. Click the "Preview" tab at the top of the block to check how it looks.
6. Save the post and check it again on the front end (by opening the site like a visitor).
There is one important detail here: in the WordPress block editor (Gutenberg), the preview of the Custom HTML block works separately from the save logic. In some versions the preview can show the iframe inside the editor without fully taking the user's unfiltered_html capability into account, yet the code is deleted at the moment of the actual save. So if you say "it showed up in the editor but vanished once I published," that is exactly a permission problem; do not let the preview mislead you. Always do the real test by opening the site on the front end.
What is the unfiltered_html capability and how do I grant it?
If you want your iframe code to be saved by users in the Editor or Author roles as well, you can grant the unfiltered_html capability to the relevant role programmatically. First, though, a serious warning: the role you grant this capability to can now save any kind of HTML, scripts included. That means someone with that role could inject harmful JavaScript into your site. Do it only for users you fully trust, and only if you really need it. As an alternative, the iframe plugins we cover later are much safer, because they allow only iframes, not all HTML.
To grant the capability, you can add the following code to the functions.php file in your theme folder (preferably inside a child theme or a site-specific plugin). This example adds the capability to the Editor role as a one-time change:
function custom_editor_iframe_permission() {
$role = get_role( 'editor' );
if ( $role && ! $role->has_cap( 'unfiltered_html' ) ) {
$role->add_cap( 'unfiltered_html' );
}
}
add_action( 'init', 'custom_editor_iframe_permission' );
Because role capabilities are written to the database, you can remove this code from the file after running it once; the capability stays. On a multisite install, the get_role approach is not enough, because WordPress additionally restricts the unfiltered_html capability on multisite through the map_meta_cap filter. To allow specific users on multisite, you need to use this filter:
function multisite_iframe_permission( $caps, $cap, $user_id, $args ) {
if ( 'unfiltered_html' === $cap ) {
// Allow only users who can edit pages
if ( user_can( $user_id, 'edit_pages' ) ) {
$caps = array( 'unfiltered_html' );
}
}
return $caps;
}
add_filter( 'map_meta_cap', 'multisite_iframe_permission', 1, 4 );
When you add this code, remember that even a single typo can make your site temporarily unreachable. Always take a backup first and, if possible, make the changes in a staging environment. Having your FTP access ready is vital so you can roll the file back if something goes wrong.
Do security plugins (Wordfence, Sucuri) filter iframes?
Say your account is an Administrator with full permissions, but the iframe still disappears. In that case a security plugin is most likely getting in the way. Plugins such as Wordfence, Sucuri, and iThemes Security (Solid Security) can scan saved content against their own rules and strip "risky" tags like iframes, or block them in a Web Application Firewall (WAF) layer.
The symptom is typical: the problem started soon after you installed or updated a security plugin. To check, look at the plugin's settings first; most have an option related to "HTML filtering" or "content sanitization". For a conclusive diagnosis, temporarily deactivate the security plugin while you try to save the iframe. If the iframe no longer disappears, you have found the culprit. Do this test only at a low-traffic moment and only for a short time, because your site is unprotected while the plugin is off.
Plugins like Wordfence may also treat requests containing an iframe as "malicious" under a WAF rule and block the save completely. In that case you can add an exception for the relevant rule in the plugin's "firewall" section (an allowlist) or loosen the rule for that URL. My general advice, though, is to use a plugin that adds iframes safely instead of fiddling with your security plugin's settings; that solves the problem without weakening your firewall.
How do X-Frame-Options and CSP headers block an iframe?
Now let us move to the second big problem: the code is there but the iframe looks empty. When you open your browser's developer console (F12), you see an error like this: "Refused to display '...' in a frame because it set 'X-Frame-Options' to 'sameorigin'" or "Refused to frame '...' because an ancestor violates the following Content Security Policy directive: frame-ancestors".
This is a security setting of the site you are trying to embed, not of your own site. X-Frame-Options and the frame-ancestors directive of Content-Security-Policy decide whether a site can be placed inside an iframe on other pages. It is a protection against an attack type called clickjacking. If the other site says "nobody may frame me" (DENY) or "only my own domain may frame me" (SAMEORIGIN), you cannot embed that site in your own WordPress page. This is not a WordPress problem, and no setting on the WordPress side can get around it.
The table below summarizes the values of these two headers and what they mean:
| Header / Value | Meaning | Can your site embed it? |
|---|---|---|
| X-Frame-Options: DENY | The site cannot be framed anywhere | No |
| X-Frame-Options: SAMEORIGIN | It can be framed only on its own domain | No (if you are on a different domain) |
| CSP frame-ancestors 'none' | No site is allowed to embed it | No |
| CSP frame-ancestors 'self' | Only its own origin can embed it | No |
| CSP frame-ancestors https://yoursite.com | Only the listed domain can embed it | Yes (if your address is on the list) |
| No header | No restriction | Yes |
In this situation you have three options. The first is to use the official embed code provided by the other site. Platforms like YouTube, Google Maps, Vimeo, and Spotify give you an iframe code from their "Share" or "Embed" menu that is specifically meant to be framed; the source address of that code (for example youtube.com/embed/ or youtube-nocookie.com instead of plain youtube.com) is set up to allow framing. The second, if the site you want to embed is under your control, is to change that site's setting (we explain how below). The third, if you truly cannot embed it, is to use a link, a screenshot, or an API-based alternative instead of an iframe.
What should I do when I embed my own site elsewhere and the other side does not allow it?
Sometimes it is the other way around: the page you want to embed is your own WordPress site, and you want to embed it somewhere else (for example on a partner's site or on your own second site), but the frame stays empty. This means your WordPress site or your server is sending an X-Frame-Options header.
As far as WordPress's documented behavior goes, it sends an X-Frame-Options: SAMEORIGIN header for the admin area (wp-admin) by default through the send_frame_options_header() function, while the front end of your site (the pages visitors see) does not get this header by default. So if the header is present on the front end, it is added either by a security plugin or by your server configuration (Apache/Nginx).
On an Apache server, look for a line like the following in the .htaccess file in your site's root directory, and change or remove it if you find it:
# Strict setting that forbids all sites from embedding (can be removed)
# Header set X-Frame-Options "DENY"
# Setting that allows only your own site
Header set X-Frame-Options "SAMEORIGIN"
# Modern way to allow one specific trusted site:
Header set Content-Security-Policy "frame-ancestors 'self' https://trusted-partner-site.com"
The critical point here is this: the X-Frame-Options header reliably supports only the DENY and SAMEORIGIN values. The old ALLOW-FROM value, meant for allowing a specific third-party site, is no longer supported in modern browsers, and browsers that see this value ignore the header entirely. If you want to allow embedding for a specific domain, the right way is to use the frame-ancestors directive of the Content-Security-Policy header. As browser documentation confirms, when a response contains both frame-ancestors and X-Frame-Options, modern browsers apply the frame-ancestors directive and ignore X-Frame-Options completely; still, it is best practice to send X-Frame-Options: SAMEORIGIN alongside it as a fallback for very old browsers that do not support CSP at all.
If you use Nginx, the same settings look like this in your site configuration file (inside the server block):
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Content-Security-Policy "frame-ancestors 'self' https://trusted-partner-site.com" always;
What is oEmbed, and when should I use it instead of an iframe?
WordPress has a very powerful feature that most users are not aware of: oEmbed. To embed content from popular platforms such as YouTube, Vimeo, X (Twitter), Spotify, or TikTok, you do not actually need iframe code. It is enough to paste just the URL of the content (for example a YouTube video link) on an empty line in the editor. WordPress recognizes the address automatically and turns the content into a clean, mobile-friendly, and safe embedded player.
oEmbed has three big advantages over copying an iframe. First, it does not require the unfiltered_html capability; even a user in the Author role can embed a video by pasting a URL, because WordPress handles it in a safe way. Second, the embed code updates automatically when the platform changes things. Third, the output is already largely responsive. So if you are embedding content from a supported platform, your first choice should always be pasting the plain URL, not iframe code.
However, oEmbed does not work everywhere. If it is your own custom application, an external form tool, a booking calendar, a PDF viewer, or a site with no oEmbed provider, an iframe is unavoidable, and you will need the other methods in this guide. The list of providers that oEmbed supports can change over time; automatic embedding may stop working when some platforms change their API policies, so always verify the output on the front end after embedding.
Can HTTP / HTTPS mixed content block an iframe?
Another cause that is often overlooked but very common is the "mixed content" problem. If your site is served over HTTPS (secure, with the lock icon) but the iframe's source address (src) starts with http://, the browser refuses to load that iframe for security reasons. In the console you see a warning such as "Mixed Content: The page was loaded over HTTPS, but requested an insecure frame", and the iframe stays empty.
The fix is simple: change the src="http://..." address in the iframe code to src="https://...". If the other site does not support HTTPS (that is, it has no secure version), you cannot embed that content in an HTTPS page; this is a hard rule set by the browser. In that case you either have to wait for the other site to move to HTTPS or find an alternative source. Protocol-relative addresses (for example ones starting with //example.com) also help here, because they use the page's own protocol.
Can my theme or page builder break the iframe?
In some cases neither the permission nor a security header is the source of the problem; the culprit is the theme you use or a page builder plugin (such as Elementor, WPBakery, or Divi). These plugins have their own content-processing layers and behave differently from the classic "Custom HTML" block.
If you use Elementor, you need to paste the iframe not into a plain text widget but into the dedicated "HTML" widget. In Divi, the "Code" module does this job, and in WPBakery, the "Raw HTML" element. If you paste it into the wrong element, the code is either converted to escaped characters or deleted. In addition, the content filters of some themes (functions hooked to the the_content filter) can break the iframe when displaying it on the front end. To test this, temporarily switch to a default WordPress theme (for example the latest Twenty Twenty-series theme) and see whether the iframe works properly. If it does, the problem is in your theme or page builder, and you need to use that plugin's correct HTML/Code element.
Why do cache and CDN not show my iframe change?
Sometimes you add the iframe correctly and it never shows up on the front end, or the opposite happens and an old iframe you deleted still sits on the screen. Before spending hours on permission and header settings, the first thing to think of at this point is caching. In the WordPress world, several cache layers work on top of each other, and each of them can delay the change you made.
The most common layers are your browser's own cache, a caching plugin such as WP Rocket or W3 Total Cache, a server-side object/page cache (for example Varnish or LiteSpeed Cache), and, on the outside, a CDN such as Cloudflare. When you save a change in the editor, the page served to the visitor may still be an old copy coming from one of these layers. That is why a good share of the cases where you say "my change does not show up" are not even an iframe problem; they are just a stale cached copy.
The right order is this: first clear all the cache from your caching plugin, then empty the CDN cache if you have one (in the Cloudflare panel, "Purge Cache"), and finally do a hard refresh in your browser (Ctrl+F5 or Cmd+Shift+R). For a reliable result, also open the page in a private/incognito window, since a private window bypasses most browser caches. If you want to know for sure whether the iframe was saved, use "view source" to look at the page's raw HTML; if the iframe tag is in the source, the problem is in the display, and if it is not, the problem is in the saving. This distinction points you to the right branch of fixes.
Should I use an iframe plugin, and which one should I choose?
If you do not want to deal with functions.php, or you do not want to take the security risk that comes with a permission change, an iframe plugin is the most practical and safe way. These plugins usually offer a "shortcode"; instead of pasting the iframe directly, you use a shortcode such as [iframe src="..." width="..." height="..."]. That way WordPress's content sanitizer does not step in, and the iframe is not deleted. On top of that, because this method allows only a controlled iframe output rather than all HTML, it is far safer than granting the unfiltered_html capability.
| Method / Tool type | Advantage | What to watch for |
|---|---|---|
| oEmbed (pasting a plain URL) | No plugin needed, safe, responsive | Supported platforms only |
| Custom HTML block | Core feature, no extra load | Requires Administrator/Editor capability |
| iframe shortcode plugin | No permission risk, code is not deleted | Choose an up-to-date, widely used plugin |
| unfiltered_html via functions.php | No plugin, permanent fix | Allows all HTML, security risk |
| Page builder HTML/Code module | Works with the visual editor | You must use the right module |
When choosing a plugin, look at three criteria: the last update date should be recent (a maintained plugin), the number of active installs should be high, and it should be marked as compatible with the WordPress version you use. An abandoned security or iframe plugin that has not been updated for years can bring a bigger vulnerability than the problem it solves. After installing it, try the plugin on a single test post and verify the result on the front end.
How do I make a responsive (mobile-friendly) iframe with CSS?
Your iframe now saves and shows up, but because it was added with a fixed width="600" height="400" value, it may overflow on mobile screens or look out of proportion. To fix this, you need to put the iframe inside a wrapper that keeps its proportions. The modern and cleanest method is to use the CSS aspect-ratio property:
<div class="responsive-iframe">
<iframe src="https://www.example.com/embedded"
title="Embedded content" loading="lazy"></iframe>
</div>
<style>
.responsive-iframe {
position: relative;
width: 100%;
aspect-ratio: 16 / 9; /* ideal ratio for videos */
}
.responsive-iframe iframe {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
border: 0;
}
</style>
Notice that here we removed the iframe's own width and height values and control the size entirely through the wrapper div. The aspect-ratio: 16 / 9 value is ideal for videos; if you are embedding a map or a form, you can change this ratio to suit the content (for example 4 / 3 or 1 / 1). To avoid rewriting this CSS every time, add it once to your theme's Additional CSS section (Appearance > Customize > Additional CSS) and then just use the <div class="responsive-iframe"> wrapper.
I also want to draw your attention to the loading="lazy" attribute. It makes the iframe load only when the user scrolls to that section; it noticeably improves your page's opening speed and is especially useful on pages with several iframes.
How do I diagnose an iframe problem step by step?
Let us turn all of this knowledge into one logical flow. To solve your iframe problem systematically, wherever you are in it, follow the order below. Check the result at each step before moving to the next, and stop at whichever step fixes the problem.
| Step | What to check | Approximate time |
|---|---|---|
| 1 | Is the code deleted on save, or does it stay but show blank? (Separate the two main groups) | 1 minute |
| 2 | Paste the iframe into a "Custom HTML" block with an Administrator account and look at the front end | 3 minutes |
| 3 | If it gets deleted: temporarily turn off the security plugin and try again | 5 minutes |
| 4 | If it is still deleted: check the user role and the unfiltered_html capability | 5 minutes |
| 5 | If it shows blank: look for an X-Frame-Options / CSP error in the F12 console | 2 minutes |
| 6 | If there is a mixed content warning, change the src address to https | 2 minutes |
| 7 | If the other site blocks it: use the official embed code or oEmbed | 5 minutes |
| 8 | If you suspect the theme, switch to a default theme and test | 5 minutes |
| 9 | If it is a visual/size problem, add the responsive wrapper CSS | 5 minutes |
The most critical step in this flow is the first one. The answer to "is the code deleted, or does it stay but not show?" sends you down two completely different paths. A deletion problem is under the control of your site and your permissions, and it can be solved. A display problem is mostly under the control of the other site, and the only thing you can do there is use its official embed method.
What should I know about iframe security and SEO?
An iframe is a powerful tool, but used carelessly it has a cost for both security and performance/SEO. On the security side, avoid embedding iframes from sources you do not trust, because a malicious iframe can be a vector that misleads users (clickjacking) or displays harmful content. If you do not trust the source you are embedding, you can add the sandbox attribute to the iframe tag to restrict what the frame can do. This also explains why WordPress treats iframes as unsafe by default and protects against them with the unfiltered_html capability.
On the SEO side, know this: content embedded inside an iframe is not counted as the text of your page. Search engines generally treat iframe content as the content of the source site. So do not put important text you want to rank inside an iframe; write it directly in the page body. Use iframes for supporting elements such as videos, maps, and forms, not to carry your main content. Also, a large number of iframes can lengthen your page load time and hurt your Core Web Vitals metrics, so the loading="lazy" advice above matters not only for speed but indirectly for SEO as well.
Frequently asked questions
Why does my iframe code get deleted when I save?
The most common reason is that the user saving the post does not have the unfiltered_html capability. For security, WordPress removes the iframe tag from users without it through the wp_kses function. The second most common reason is a security plugin (such as Wordfence or Sucuri) filtering the iframe. Using the "Custom HTML" block with an Administrator account, or choosing a safe iframe plugin, fixes it.
The iframe shows in the editor but disappears after I publish. Why?
In some versions the block editor's preview can show the iframe temporarily, but the user's capability is checked at the actual save, and if it is missing, the code is deleted. That is why what you see in the preview can be misleading. Always do the real test by opening the site on the front end like a visitor.
The iframe code is there but the frame looks empty. What is wrong?
This is usually because the site you are trying to embed forbids being framed with an X-Frame-Options or Content-Security-Policy frame-ancestors header. In the browser console (F12) you see a "refused to connect" or "X-Frame-Options" error. This is not a WordPress problem; you need to use the other site's official embed code.
How do I let Editor or Author users add iframes?
You can grant the unfiltered_html capability to the relevant role with code added to functions.php. But this lets that role save all HTML (scripts included) and carries a security risk. The safer way is to use a shortcode-based iframe plugin that allows only iframe output.
Do I need iframe code to embed a YouTube video?
No. With WordPress's oEmbed feature, you just paste the YouTube video link as plain text on an empty line, and WordPress turns it into a safe, mobile-friendly player automatically. This method does not require the unfiltered_html capability, and even users in the Author role can use it.
Why does the iframe not work at all on WordPress multisite?
On multisite installs, the unfiltered_html capability belongs only to the Super Admin account by default; even regular site administrators cannot save iframes. To allow specific users, you need to add custom code that uses the map_meta_cap filter or install an iframe plugin.
I cannot embed my own site in another site. How do I remove the block?
Your site's server or a security plugin is sending an X-Frame-Options header. You can adjust this header in .htaccess on Apache or in the server configuration on Nginx. To allow one specific trusted site, use the frame-ancestors directive of Content-Security-Policy instead of the no longer supported ALLOW-FROM.
The iframe overflows on mobile. How do I make it responsive?
Remove the iframe's fixed width and height values, put it inside a wrapper div, fix the aspect ratio with aspect-ratio in CSS (for example 16/9), and set the width to 100 percent. This keeps the iframe in proportion and fitting on every screen size, and adding loading="lazy" also improves page speed.
