Codeable info

Two Ways to Embed HTML in WordPress

Posted on by in WordPress Tutorials

Do you have an already written HTML snippet you would like to include in your WordPress blog or website but are having trouble? We can help! It would certainly save a lot of time if there was an easy, clever way to embed the code without having the WYSIWYG editor destroy the original HTML code, right? I certainly think so! That’s why I wrote this tutorial about two ways to include existing HTML in a WordPress post or page. It’s not too difficult, so let’s begin.


One quick and easy way to embed regular HTML code inside a WordPress post is to use the EmbedIt WordPress Plugin. The plugin works with Meta tags by using the built in WordPress custom keys feature. Those of you using WordPress 3.1 or higher may be a little confused as to where to locate the custom keys feature because they no longer show custom keys in post editing pages since version 3.1, so we will explain how to remedy the situation in the plugin instructions. Here is how to use the EmbedIt plugin in half a dozen simple steps:

  1. Download the plugin to your desktop from:
  2. From your WordPress dashboard, go to “Plugin”, click on “Add New” and then click “upload” in the top navigation bar of that page.
  3. Click the “Browse” button to locate the .zip file with the plugin on your desktop. Then click on “install now” and you are ready to use the plugin!
  4. Add a new post in which you will add your HTML code snippet to. Wherever you want the HTML snippet to start, place the text “HTML1” in square brackets as in the following screenshot:
  5. HTML1

  6. If you are using WordPress 3.1 or higher, activate the custom fields feature by clicking on “Screen Options” in the upper right of your add posts page and putting a check mark by “custom fields”. See the example image below to help you find the custom fields option:
  7. custom fields

  8. Now create a custom field named “HTML1” without the quotes. Then cut and paste your HTML snippet into the value field of the custom field and you are done! Here is a screenshot of the process when we did it:

That’s one easy way to include HTML snippets into your WordPress site. This technique is great for videos that use embed tags such as ones from YouTube and other sites. It can also be used for just about any other HTML code you have trouble inserting with the WordPress WYSIWYG editor.


While it is true that many code snippets can be inserted into a WordPress site using the above mentioned EmbedIt plugin, there are some situations or code structures to watch out for. You don’t want to put any HTML tags in your site that should not be repeated or accidentally close any open tags from the WordPress Theme’s header.php file or other template files. To avoid this you should strip your HTML code snippet of any of the following tags:

  • HTML tags, i.e.: or normally close to the top of the file and at the very end normally. You should always delete these.
  • Body tags such as , or will again be close to the top and bottom of the file. You should always delete these as well.
  • Head tags like: or should be deleted, but probably not everything in between them (see below).
  • Title tags which would be either and anything text in between them can be safely deleted.
  • Meta tags such as

Those are all probably all of the tags you can simply just delete. Only in some cases can you also delete what is in between the opening tag and closing tag. For example, you should always be able to safely delete what’s in between the opening title tag and closing title tag, but you would not want to do the same with the body tag obviously or you wouldn’t have any code left!


If you have CSS in your HTML snippet that you plan to use with the EmbedIt plugin, then there are more special instructions for you to follow yet. If there is anything other than inline styles in your code snippets tags, you will want to rewrite it or move it. By inline styles, we mean ones that are inside of tags. An example of this situation would be a div tag with the style attribute set like:

 <div style=”color:red”>

That would be fine. However there are two other ways of including CSS styles in your HTML that will not work so good with WordPress and the EmbedIt plugin. CSS styles can also be included in the head section of the HTML page or linked to from within the head section of the HTML. Either way is going to require a little work unless you get lucky and it looks good.


Internal styles, or styles defined in the head of your HTML document or code snippet will need to be moved. Now the correct way to do this would be to convert them all to inline styles defined in each individual tag, but that could take forever and be quite complex as well, so there is another option. The other way to do it is explained here:

  1. Remove the head, body and title tags from the top of the document and there closing tags and any other tags explained above that can be removed.
  2. Cut all the text from the opening style tag to the end of the closing style tag and paste it to right after where the body tag was before you removed it. It should basically be the first thing in your HTML code snippet.

That’s how you remedy styles defined in the head of the document, but if they are linked to in the head you will do it like this:

  1. Locate the CSS file that the link points to.
  2. Delete all unwanted tags from your HTML snippet as explained above.
  3. Insert opening and closing style tags at the beginning of the cleaned up HTML code snippet. They can simply look like this:


  4. Now copy your entire CSS file to your clipboard by using select all and copy.
  5. Paste the contents of your CSS file in between your opening and closing style tags.

Now you should have a code snippet that will work with the EmbedIt plugin and WordPress You will often have to make some style changes to make it look like it did before installing it into WordPress because WordPress default or custom styles may affect your code in ways you don’t like or want. Play with the CSS within the HTML code snippet until it looks like you want it to. If you don’t have any style in your code snippet and it looks bad, you can add style to it just like we did for adding the external style sheet style at the top of the HTML code. Just type in your opening style tag, define your styles and type the closing style tag and you are done. Test, fix test…


If you use a plugin, EmbedIt is probably one of your best options. Some People, including myself sometimes, just don’t like to use too many plugins. For those of you who don’t want to use the EmbedIt plugin and don’t want to alter the code to be compatible with the method explained above, there is another option, iFrames. I know some of you cringe at the thought of iFrames while some people love them. iFrames have their place in HTML though and they really are not as bad to use as some people make them out to be, especially in WordPress where they are a solution to some problems rather than a cause of a problem. For one thing, they are much easier than using the EmbedIt plugin described in this tutorial to insert an HTML snippet in your WordPress site. Perhaps the best part about using an iFrame instead of a plugin is that you can leave your CSS styles intact. For those of you that don’t mind iFrames or are at least willing to give them a shot, we are going to show you how to set a home page and put an iFrame tag in it.


First, to be able to include our iFrame example in the home page of the site, we will quickly demonstrate how to set a page as a home page in WordPress so that it stays the home page:

  1. Go to pages and name an empty page something like “Home” or “Home Page”.
  2. Add whatever header and other content you want that will go before your iFrame content.
  3. Publish the page.
  4. Go to your WordPress dashboard, hover over “Settings” on the left sidebar and click on “Reading”. On top of the “Reading Settings” page where it says “Front page displays”, select “a static page” and then select the name of the page you created in step one.
  5. Save your changes.


Here is how to include your HTML snippet in your WordPress home page using HTML iFrame tags:

  1. Pick a spot in your page or post where you want your frame to appear. You can include an iFrame with no border and no one will be able to tell there is an iFrame in your page.
  2. There is a magically little code snippet we use to include iFrames. It needs to go in the functions.php file in your theme’s folder. Here is the code to copy and paste there
    // make TinyMCE 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;') );
  3. That code will force the TinyMCE WYSIWYG editor in WordPress to allow iFrames.

  4. To include the iFrame in the WYSIWYG editor while making your page, go to the spot on the page that you want the iFrame to appear and enter the following code:
    <iframe src="//" width="550px" height="450px">
      <p>Your browser does not support iframes.</p>
  5. Save your edited home page and preview it to see that it works and then you can publish it or finish editing. You are all done!

    The reason you need to edit the functions.php file to allow iFrames is because the WYSIWYG editor doesn’t know how to treat them otherwise. We tried to use an iFrame in a very basic theme without editing the functions.php file first and while the iFrame did indeed display, the code was wrong and it also displayed the text that was only supposed to show if the user’s browser will not support iFrames (the HTML code in between the opening iFrame tag and closing iFrame tag). So if you use this little trick described above, it won’t do that. If it does, go to the HTML view and fix it and hopefully the next time you open WordPress to edit the page it will not do it again. Sometimes if you are use a caching system, the change won’t take effect right away.


    IFrames allow you to do all sorts of things that WordPress won’t allow you to do using regular PHP, HTML, ASP, JavaScript or other website coding languages. You simply place the page containing the code that won’t work in WordPress in a directory outside of the WordPress directory and link to it in an iFrame! Using the EmbedIt plugin often works as well, so try whichever method appeals to you the most. Good luck with your next WordPress project and happy coding!

Codeable info

Comments (3)

Comment by Yusuf says:

Thank you! Great help!

Comment by vadim says:

Correct me if I am wrong but all these suggestions are meant for (self-hosted solution). People casually call wordpress both wordpress.COM and wordpress.ORG and it may cause tons of confusion. Many things that are available for .ORG users are hidden from .COM users. Took me a while to figure out why I couldn’t follow these simple instructions. E.g. there is no Plugin tab in .COM dashboard:

Comment by Maria says:

Hi Ian,

Thanks for this tutorial. It’s very helpful.


Codeable info