How to Format HTML Code in WordPress Blog Posts
HTML allows you to add custom functionality and formatting to your WordPress blog posts. Whether you’re a developer writing tutorials or a blogger customizing a layout, there are times when inserting raw HTML code into a WordPress post is necessary. However, if not formatted correctly, WordPress may alter, strip, or even block your HTML entirely. In this guide, we’ll walk you through the correct methods to safely and effectively include HTML code in your WordPress content.
Table of Contents
- Introduction
- Common Problems with Pasting HTML in WordPress
- Ways to Add HTML Code in WordPress Blog Posts
a. Using the Code Block in Gutenberg Editor
b. Using Preformatted Block
c. Classic Editor: Switching to ‘Text’ Mode - Prevent WordPress from Stripping or Modifying HTML
- Displaying HTML Code as Text for Tutorials
- Best HTML Syntax Highlighting Plugins
- Bonus Tips for Clean HTML Formatting
- Formatting HTML Code in Comments (Optional)
- Troubleshooting Common Issues
- Conclusion
Common Problems with Pasting HTML in WordPress
One of the first challenges users face when inserting HTML code is that WordPress doesn’t always render it exactly as expected. The default editor especially the Gutenberg block editor tries to optimize and sanitize the content. This can lead to unexpected changes, such as the removal of certain tags or the automatic addition of paragraph breaks and line breaks. Users often switch back and forth between the visual and code editor, only to find that their HTML changes aren’t preserved.
This behavior is mostly due to WordPress’s internal content filters, such as wpautop
and wp_kses
, which are designed to improve readability and security. However, they can be frustrating when you’re trying to display or embed custom HTML. These automatic filters can especially impact complex structures like tables, forms, or inline scripts.
Ways to Add HTML Code in WordPress Blog Posts
There are multiple ways to include HTML code in a WordPress post, depending on your use case and the editor you use.
Using the Code Block in Gutenberg Editor
The Gutenberg editor offers a “Code” block designed specifically for inserting snippets of code. This block is ideal when you want the HTML code to be visible to the reader (as in tutorials), not rendered. To use it:
- Open your post in Gutenberg
- Click the “+” button to add a new block
- Select the Code block
- Paste your HTML inside this block
This method keeps your HTML safe from WordPress formatting and filters, and it ensures your code is displayed as-is.
Using Preformatted Block
The Preformatted block is another option that maintains the formatting of your pasted HTML, including line breaks and indentation. This block works well when you need both the raw HTML structure and visual spacing preserved. However, unlike the Code block, it may not always differentiate between code and plain text, making it less ideal for tutorials.
- Add a Preformatted block
- Paste your code
- Ensure the output matches your intent
Classic Editor: Switching to ‘Text’ Mode
If you’re using the Classic Editor, there’s a “Text” tab (also called the HTML tab) alongside the “Visual” tab. This mode lets you paste your HTML directly without interference. It’s especially useful for custom embeds, scripts, or minor layout adjustments.
- Switch to the Text tab in the Classic Editor
- Paste your HTML
- Switch back to Visual only if necessary (this may alter code)

Prevent WordPress from Stripping or Modifying HTML
WordPress uses various filters to sanitize content, which is useful for security but problematic when adding valid HTML. There are a few ways to prevent WordPress from modifying your code:
- Use the Code or Text editor modes as described
- Disable
wpautop
manually or using a plugin. - Use custom shortcodes like
[code]...[/code]
with an appropriate plugin - Avoid pasting code in Visual mode to reduce auto-formatting
Developers can also hook into WordPress functions to allow specific tags or attributes, though this requires some coding knowledge.
Displaying HTML Code as Text for Tutorials
When you’re writing a guide and want to display HTML code without having it executed or interpreted, you need to escape special characters. WordPress otherwise tries to render the code, which is not ideal for educational content.
To display HTML as plain text:
- Wrap the code in
<pre>
or<code>
tags - Escape special characters like
<
,>
,&
,"
,'
- Use online HTML escaping tools to automate this
Example Before Escaping:
<div class="box">Content</div>
Example After Escaping:
<div class="box">Content</div>
Best HTML Syntax Highlighting Plugins
To improve readability and style when showing code examples, use syntax highlighting plugins. These plugins add color and formatting to your code blocks:
- SyntaxHighlighter Evolved – Adds formatting and supports many languages
- Prism.js plugin – Lightweight and customizable syntax highlighting
Choose one based on your theme compatibility and performance needs.
Formatting HTML Code in Comments
If you run a community-driven blog and want users to share code in the comments, you should limit the HTML tags allowed to avoid spam or security risks. By default, WordPress allows only safe tags in comments.
- Use plugins to allow limited HTML or
<code>
tag usage - Display a note for commenters on how to format code
- Consider using a plugin like Crayon Syntax Highlighter for Comments
FAQs
1. Can I safely add <script>
or <iframe>
tags in WordPress posts?
By default, WordPress restricts certain tags like <script>
, <iframe>
, and <object>
due to security concerns. If you’re an admin, you can bypass this restriction by editing the theme’s files or using a plugin that allows specific tags. Otherwise, WordPress will strip these tags to prevent potential XSS attacks. A safer alternative is to use a plugin built specifically to embed external scripts or content (like video or maps).
2. How can I add HTML code without using any plugins?
If you prefer not to use plugins, the safest approach is to switch to the Code block in Gutenberg or the Text tab in the Classic Editor. You can also manually encode your HTML using online HTML escape tools. This way, WordPress won’t interpret the code and will show it as plain text.
3. Does using raw HTML slow down my WordPress site?
No, using raw HTML itself does not slow down your site. However, excessive inline styles, scripts, or outdated HTML practices (like nested <table>
structures) can affect performance. For optimal speed, keep your HTML lean and avoid unnecessary markup, especially when editing within the post content.
4. How can I prevent WordPress from adding extra <p>
and <br>
tags to my HTML?
To stop WordPress from automatically inserting paragraph or line break tags, you can disable the wpautop
filter. This requires adding a small function to your theme’s functions.php
file or using a plugin designed to disable automatic formatting. Alternatively, using <pre>
or <code>
blocks helps avoid this issue entirely.
5. Can I highlight only a portion of HTML code within a sentence?
Yes, you can use inline <code>
tags to highlight a small portion of HTML within a sentence. However, this works best when you manually escape the special characters. For example:
To style a div, use the <code><div class=\"container\"></code> element.
This ensures that the snippet is readable, styled correctly, and doesn’t break your content structure.
Build a Stunning Website in Minutes with TemplateToaster Website Builder
Create Your Own Website Now
How we can create website without coding?
Thаnks fоr finaⅼly writing aboᥙt > How to correctly format ɑnd use HTML
code in WordPress blog posts marketing automation
The section on escaping code for tutorials was exactly what I needed. I’ve been manually typing character entities, but those tools you suggested make it way easier. Appreciate the thorough breakdown.