WordPress Plugin: Displaying Code in Posts

If your wordpress site aims to deliver tech info like mine does, you probably need to display code snippet, like so:

// what's wrong with this code?
int main() {
int i = 0;
    while (true) {
        if (i = 10) return 0;
        i++;
    }
}

 

I have tried various plugins and have done some research. In the end, I decided to use SyntaxHighlighter Evolved  by Alex Mills. Click here for plugin page.

It’s simple to use and has great language support, including common ones such as c++, java, python, etc. All you need is to include short codes in your posts, which are short piece of code that allows great capabilities with little effort, such as the following:

[programming_language]
//your code here...
[/programming_language]

The plugin comes with various syntax coloring:

Default

Django

Eclipse

Emacs

Fade to Grey

Midnight

RDark

You can provide additional options by setting the attributes of the short codes. To set first line number to 5:

[programming_language firstline="5"]
print("Hello")
print("My name is CK.")
[/programming_language]

print("Hello")

print("My name is CK.")

Highlight some line of code:

[programming_language highlight="1,2,5"]
print("Hello")
print("My name is CK.")
print("Go Bears!")
print("Go More Bears!")
print("Go Many Bears!")
[/programming_language]
print("Hello")
print("My name is CK.")
print("Go Bears!")
print("Go More Bears!")
print("Go Many Bears!")

Documentation on the plugin can be found here.

Any comment? Opinion? Suggestion? Leave a comment!

Leave a Reply

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