Code in WordPress Posts

Have you ever tried to put a snippet of HTML/PHP/etc in a tutorial you were writing? The best way is not entirely obvious.

Many will recommend using a plugin for the simple act of displaying code snippets. I recommend, instead, doing it manually. Here’s what you need to do:

Note: I assume you are using the WYSIWYG “visual editor”. If you’re using the other one, the steps will be similar, but there may be be some differences. If you’re not using the visual editor, then chances are you already know how do this.

  1. Open your preferred text editor.
  2. Write your code snippet in it.
  3. Use the Replace tool to change every instance of ” to ". Do the same with angle brackets, and any other symbols that need to be replaced with character entities. Technically, you can just paste everything into the WordPress WYSIWYG editor (and it will convert everything), but you will still need to replace the quote marks with "s.
  4. Copy and paste the code into the WordPress editor.
  5. Switch to the “Code” tab, and put some <code> tags around your pasted snippet.

That’s it. Do you really need a plugin for that?