Developer · Guide
How to Add HTML in WordPress (Posts, Pages, Widgets, Templates)

Short answer
To add HTML in WordPress, insert a Custom HTML block in the block editor and paste your code, or select any block and choose Edit as HTML to change its markup. To edit the whole post as HTML, switch to the Code editor from the Options menu. For site-wide areas like the footer, add a Custom HTML block to a template part or widget area.
Key takeaways
- The Custom HTML block is the main way to add your own HTML to a post or page.
- Edit as HTML changes one existing block, while the Code editor shows the whole post as markup.
- Template parts and widget areas let you place HTML on every page, such as in the footer.
- WordPress filters some HTML, like scripts and iframes, for users without full HTML permission.
- Validate and preview your HTML, because broken tags can upset the layout of the whole page.
Where can you add HTML in WordPress?
You can add HTML inside posts and pages, inside a single block, across the whole post in the code editor, in widget areas, and in theme templates.
Choose the place based on where the HTML should appear:
| Where it should appear | Method |
|---|---|
| One spot in a post or page | Custom HTML block |
| Tweak an existing paragraph, image or button | Edit as HTML on that block |
| Edit the full post markup | Code editor view |
| Sidebar or footer on every page | Widget area or template part |
| A layout used by many pages | Template in the Site Editor |
| A separate stand-alone page | Upload an .html file or use a blank template |
For scripts and tracking tags, see our guide to adding code to WordPress, since those usually belong in the site header or footer instead.
How do you use the Custom HTML block?
Click the plus button in the editor, add the Custom HTML block, paste your HTML, then use the Preview tab inside the block to check it.
Step by step:
- Open the post or page in the editor
- Click where you want the HTML and press the plus button
- Search for Custom HTML and select it
- Paste or type your HTML
- Click Preview in the block toolbar to see the result
- Save or update the post
The Custom HTML block documentation covers the block's options. You can also type /html on an empty line to insert it faster.
The block keeps your markup as written, which makes it good for tables with special formatting, embed codes and small widgets from other services.
How do you edit an existing block as HTML?
Select the block, open the three-dot options menu in the block toolbar and choose Edit as HTML, change the markup, then click away to switch back.
This is handy for small changes, such as adding an id for an anchor link or a rel attribute on a link.
Be careful with block markup. If your edit no longer matches what the block expects, WordPress shows an “unexpected or invalid content” warning. You can then convert it to a Custom HTML block or undo the change.
For anchor links, you usually do not need HTML at all. Most blocks have an HTML anchor field under Advanced in the block settings.
How do you edit the whole post in HTML?
Open the Options menu at the top right of the editor and choose Code editor, or press Ctrl+Shift+Alt+M on Windows or Cmd+Shift+Option+M on Mac.
The code editor shows the full post as HTML with block comments, which look like HTML comments starting with wp:. Those comments tell WordPress where each block begins and ends, so leave them in place.
Use the code editor for careful bulk changes, such as replacing many links at once or cleaning up pasted formatting. Switch back with Exit code editor.
If you use the classic editor plugin, the Text tab plays the same role. It shows the raw HTML of the post, and switching back to the Visual tab can tidy up or remove some markup, so make HTML edits last.
How do you add HTML to the sidebar or footer?
In a block theme, add a Custom HTML block to the footer or sidebar template part in the Site Editor; in a classic theme, add a Custom HTML widget under Appearance > Widgets.
For block themes, go to Appearance > Editor, open Patterns or the template that contains your footer, and select the footer template part. Add a Custom HTML block where you need it and save. The change applies to every page using that part.
For classic themes, open Appearance > Widgets, choose the widget area and add a Custom HTML block or widget there.
Our guide to editing the header in a block theme uses the same steps for the top of the site.
How do you add HTML to a template?
Open the template in Appearance > Editor > Templates, add a Custom HTML block where you want it, and save, so every page using that template shows the HTML.
This suits elements that belong on a type of page, such as a notice on every blog post or a banner on category archives.
Keep page-specific content out of templates, and note in the template name what you added. A template change affects every page that uses it, which is great for consistent elements and confusing for one-off ones.
Our guide to creating a WordPress template explains how templates and template parts fit together.
How do you style the HTML you add?
Give your HTML a class name and style it in Additional CSS, rather than writing inline styles on every element, so the design stays consistent and easy to change.
Inline styles, written as a style attribute on each tag, work but quickly become hard to manage. Changing a color later means editing every snippet by hand.
A cleaner approach:
- Add a class to your outer element, such as class=“promo-box”
- Open Appearance > Editor > Styles and choose Additional CSS, or Appearance > Customize in a classic theme
- Write rules for that class, such as padding, border and background
- Save and check the page on desktop and mobile
Use your theme's color and spacing variables where possible. Block themes expose them as CSS custom properties generated from theme.json, so your HTML matches the rest of the site even if the theme's palette changes.
Keep class names specific to avoid clashes. A generic name like box or button may already be used by your theme or a plugin and pick up styles you did not expect.
Why does WordPress remove some of your HTML?
WordPress filters HTML for safety, so users without the unfiltered_html capability lose tags such as script, iframe, form and some attributes when they save.
The filter uses an allow list of safe tags and attributes, documented in the wp_kses_post reference. Paragraphs, headings, links, images, lists and tables pass through. Scripts and many embed tags do not.
On single sites, Administrators and Editors have unfiltered_html by default. On multisite networks, only Super Admins do, so even site administrators see their scripts removed.
If an embed is stripped, check whether WordPress has an Embed block for that service. Embed blocks need only the URL and are not filtered.
How do you check that your HTML is correct?
Preview the page, view it on mobile, and run tricky markup through an HTML validator, because a missing closing tag can break the layout below it.
A single unclosed div or table tag can push the sidebar under the content or swallow the footer. The W3C Markup Validation Service points out errors like this in seconds.
Also check accessibility basics while you are there:
- Images have meaningful alt text
- Headings follow a logical order and are not used just for big text
- Links have descriptive text instead of “click here”
- Tables have header cells
MDN's guide to HTML basics is a good refresher if you write HTML by hand.
When should you not use custom HTML?
Avoid custom HTML when a block already does the job, because blocks are easier to edit, adapt to your theme's styles and work better on mobile.
Buttons, columns, tables, galleries, quotes and embeds all have blocks. They inherit your theme's colors and fonts automatically, while hand-written HTML keeps whatever styles you gave it.
Custom HTML also makes content harder for other editors on your team to change safely. Use it for what blocks cannot do, and keep each snippet small and clearly labelled.
If you find yourself adding the same HTML on many pages, turn it into a pattern instead. You can then insert it anywhere and keep it consistent.
Frequently asked questions
How do I add HTML code to a WordPress page?
Open the page in the editor, add a Custom HTML block where you want the code, paste your HTML and preview it. Then click Update or Publish to save the page.
Can I edit HTML in the WordPress block editor?
Yes. Use Edit as HTML on a single block, or switch to the Code editor from the Options menu to see and edit the whole post as HTML, including the block comments.
Why does WordPress delete my HTML code?
WordPress removes tags like script and iframe for users without the unfiltered_html capability. It may also rewrite markup that does not match a block. Use the Custom HTML block and an administrator account for scripts.
How do I add HTML to every page in WordPress?
Add a Custom HTML block to a template part, such as the header or footer, in the Site Editor. In classic themes, use a Custom HTML widget in a widget area that appears on every page.
Can I upload an HTML page to WordPress?
Yes. You can upload a static HTML file to your server by FTP or file manager, or paste the HTML into a Custom HTML block on a page that uses a blank template. Keep its links and assets in mind.
Sources
- Custom HTML block WordPress.org Documentation
- wp_kses_post() WordPress Code Reference
- Markup Validation Service W3C
- Creating the content MDN Web Docs
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
Related guides
About the editors
Researched from the primary sources listed above and checked against our editorial rules before publishing.


