How to add sample programming code to a post or page in WordPress

Double quotes

When you write a post or create a page you may want to add sample program snippets to it. If your code contains double quotes (“) the WordPress page will display different characters for the opening and closing quotes, like in this   “example”.

Unfortunately these characters differ from the quotes you use for programming, so if someone pastes the sample code into a code editor the compiler will not understand them.

To force WordPress to display your lines as those were intended

  • Paste the sample code into the article in Visual mode
  • Switch to Text mode and place <code> and </code> tags around the code snippet

 HTML or XML

When you insert HTML or XML code into you page make sure you keep the order described above. The visual editor will convert the < and > characters to &lt; and &gt;, so the WordPress engine will not interpret your sample code as part of the HTML of the page.

If you enter the following in Text mode

<code>
<html>Text</html>
</code>

WordPress will remove your < and > characters and the page will display

Text

Leave a comment

Your email address will not be published. Required fields are marked *