Install a Plugin to Display Code in WordPress Posts
The other alternative is to use a WordPress Plugin. To know more about plugins, check out How to Install, Remove and Configure Plugins in WordPress.
There are many plugins which allow you to insert and display code correctly on WordPress posts. Some of them are Syntax Highlighter, Syntax Highlighter Plus, WP Advanced Code Editor and Google Syntax Highlighter.
Of these, the simplest and the best plugin is the Syntax Highlighter plugin. It is easy to use and configure, and it allows you to display many languages in a visually pleasing way.
Download the Syntax Highlighter Plugin from WordPress Plugins.
Upload it on your wordpress/wp-content/plugins directory. Go to your WordPress Dashboard > Plugins and activate the Syntax Highlighter Plugin.
Once your plugin is activated, you can configure the plugin settings from Settings > Syntax Highlighter.
Now to insert code into your WordPress posts, just type whatever code you want in the Visual View of the WordPress Post Editor.
After you have inserted your code, just use these tags to enclose that code.
For example, if you insert PHP code, just enclose your code in these tags: [php][/php]
echo "Hi, this is PHP code";
[/php] The Syntax Highlighter plugin supports many other languages:
- PHP — php
- Python — python
- XML — xml
- HTML - html
- C++ — cpp
- C# — csharp
- CSS — css
- Java — java
- Ruby — ruby
- SQL — sql
- Visual Basic — vb
- Delphi — delphi
Just insert your code, use the appropriate enclosing tag and your code will be displayed visually.