How to Edit HTML in WordPress Posts and Pages
In this article
Editing HTML directly in WordPress gives you precise control over the layout, styling, and embedded content of your posts and pages. This guide shows you how to switch to the HTML view in both the Block (Gutenberg) editor and the Classic editor, make your changes, and avoid common pitfalls that can break your site.
What You'll Need
- Access to the WordPress admin dashboard of the site you want to edit.
- At least Editor or Administrator role (or a custom role with edit permissions).
- A basic understanding of HTML tags (e.g.,
<p>,<div>,<img>) to avoid accidental breakage.
Step-by-Step
In the Block Editor (Gutenberg)
- Log in to your WordPress admin area and navigate to Posts > All Posts or Pages > All Pages. Click the title of the post/page you want to edit.
- Click on the block you want to edit (e.g., a paragraph, heading, or image block). You'll see a toolbar appear above it.
- Click the three-dot menu (⋯) in the block's toolbar, then select Edit as HTML. The block's content will now show raw HTML.
- Make your HTML changes directly in the text area. You can add tags, attributes, or even new elements within that block.
- To return to the visual view, click the three-dot menu again and choose Edit visually.
- If you need to edit the entire page's HTML (including the wrapper divs), click the three-dot menu in the top-right corner of the editor, then select Code editor. This switches the whole editor to a code view.
- After making changes, click Update (or Publish if it's new) to save your changes.
In the Classic Editor
- Go to Posts > All Posts or Pages > All Pages and open the post/page you want to edit.
- In the editor, look for two tabs above the content area: Visual and Text. Click Text.
- You'll now see the raw HTML of your content. Make your edits directly in this text area.
- When you're done, click Update or Publish.
Note: If you don't see the Text tab, make sure the Classic Editor plugin is active, or that your site is set to use the Classic editor under Settings > Writing.
Common Mistakes
- Breaking the HTML structure: Missing a closing tag (like
</div>) can cause layout issues across the entire page. - Using unescaped ampersands: In HTML, write
&for an ampersand (&) to avoid validation errors. - Editing the wrong block: In the Block editor, if you click “Edit as HTML” on a paragraph block, you only edit that block, not the whole page. Users often expect the whole page to be editable from there.
- Pasting from word processors: Copying from Word or Google Docs can bring in messy inline styles and stray tags. Use “Paste as plain text” first.
- Not backing up: A small error can break the layout. Always have a backup or use a staging site.
Tips
- Use the Code editor (Block editor) or Text tab (Classic) to see the full HTML of your content. This is especially useful for finding hidden issues.
- For complex custom HTML (like embedding forms or custom scripts), consider using a Custom HTML block in the Block editor. It keeps your code separate and easier to manage.
- If you need to add HTML to the
<head>(like meta tags), don't edit the post/page. Use a plugin like “Insert Headers and Footers” or add it to your theme'sheader.php. - Test your changes in a private browser window or on a staging site before publishing to avoid showing broken code to visitors.
- Use the browser's developer tools (F12) to inspect the element and see the exact HTML structure if you're unsure what to edit.
Troubleshooting
- My changes don't appear on the site: Clear your browser cache and any caching plugins (like WP Super Cache or W3 Total Cache). Also check if your theme or a page builder is overriding the content.
- The layout is broken after editing: Undo your last change by clicking the “Undo” button in the editor (or press Ctrl+Z). If you've already saved, restore a backup or use the revisions feature in WordPress (click “Revisions” in the edit screen).
- I can't find the Text tab in the Classic editor: The Classic Editor plugin may not be installed, or your site is using the Block editor by default. Install the Classic Editor plugin from Plugins > Add New.
- HTML code is being stripped or reformatted: WordPress automatically removes some tags (like
<script>) if you don't have the right permissions. Use a plugin like “Custom HTML” or add the code via a widget or theme file instead. - My custom HTML shows as plain text: Make sure you're not in the “Visual” tab when pasting. In the Block editor, use a “Custom HTML” block, not a paragraph block.
Frequently Asked Questions
Can I edit the HTML of the entire page in the Block editor?
Yes, click the three-dot menu in the top-right corner and select “Code editor”. This shows the full HTML of the entire post/page, including the block wrapper elements.
Why is my HTML being stripped when I save?
WordPress sanitizes content for users who don't have the “unfiltered_html” capability. Administrators on single-site installs usually have this, but on multisite, only Super Admins do. If you're not an admin, you'll need to ask one to add your code.
How do I add custom HTML to the head of my site?
You can't do that from the post/page editor. Use a plugin like “Insert Headers and Footers” or edit your theme's header.php file.
What's the difference between the Block editor and Classic editor for HTML editing?
The Block editor allows you to edit HTML per-block or in a full code editor, while the Classic editor has a simple Visual/Text tab toggle. The Block editor is more modern and recommended for new sites.
Can I edit HTML in a page built with a page builder (like Elementor)?
Yes, but you need to use the page builder's own HTML widget or code element. Editing the post/page HTML directly may not work because the page builder stores its own data.
Will editing HTML affect my SEO?
If you add proper schema markup or improve the structure, it can help. But if you break the HTML, it can hurt your SEO. Always validate your changes.