How to highlight code in WordPress

WordPress Editing Tabs

Although it’s easy to use WP Chili out of the box, WordPress does have some limitations, like the following:

  • you need to change to the Code editor before adding a snippet to your post
  • you need to make your snippet postable yourself, escaping all HTML entities
  • even if snippets are very short, they are intermingled with their explanations and it’s tricky to concentrate on writing the best post with so many distractions in between

Using WP Chili & Enzymes

Now I’ll show you a better approach by using together WP Chili 1.0 and Enzymes 2.2.
All you need to be up and running is the classical roundtrip: download, unzip, upload, and activate.

How to highlight code snippets

You can start by writing a simple enzyme for automating things, like the following hilite custom field: {[ .hilite | .hilite( =php= ) ]}

hilite can be used with the following patterns:

  1. {[ =snippet= | .hilite( =language= ) ]}
  2. {[ .snippet | .hilite( =language= ) ]}

The first pattern comes in handy when you want to highlight some very short and naive snippet
{[ =echo htmlentities( $name );= | .hilite( =php= ) ]} renders {[ =echo htmlentities( $name );= | .hilite( =php= ) ]}

So far, so good, but if the snippet has a character that WP texturizes, then hilite seems to fail. In fact
{[ =$welcome = “Hello “.$name;= | .hilite( =php= ) ]} renders {[ =$welcome = “Hello “.$name;= | .hilite( =php= ) ]}

The above issue is not a hilite‘s bug but it could be fixed by adding new code to it, or with new enzymes along the pathway, like the following requote custom field: {[ .requote | .hilite( =php= ) ]}

which makes that
{[ =$welcome = “Hello “.$name;= | .requote() | .hilite( =php= ) ]}
renders {[ =$welcome = “Hello “.$name;= | .requote() | .hilite( =php= ) ]}

The best option is to add another custom field for hosting your snippet and use the second pattern: in fact hilite‘s snippet has been rendered by {[ .hilite | .hilite( =php= ) ]} and requote‘s one by {[ .requote | .hilite( =php= ) ]}.

How to highlight code files

Sometimes you have a file that you want to show in its entirety, and having to copy it into a custom field is annoying or maybe not an option, if the file is alive, for example.

In such cases you can use an enzyme like the following file custom field: {[ .file | .hilite( =php= ) ]}

It’s use is again very simple and very similar to the above patterns:
{[ .file( =blog/wp-content/plugins/hello.php= ) | .hilite( =php= ) ]} renders {[ .file( =blog/wp-content/plugins/hello.php= ) | .hilite( =php= ) ]}

WP Chili Released

A couple of days ago I released WP Chili, a simple WordPress plugin that installs Chili into WordPress and let’s you add client-side code highlighting to your posts, with extreme simplicity.

After activating WP Chili, it’s just a matter of wrapping your snippets into code or pre-code elements, with the programming language of the snippet as a class, like in the following example:

{[ .example-pre-code1 | 192.hilite( =html= ) ]}

When the post reaches your readers, the snippet is unescaped by their browser and highlighted by Chili 1.9 (downloaded from your server). Your readers will see the example rendered as:

{[ .example-pre-code2 | 192.hilite( =php= ) ]}

Enzymes 2.2 Released Today

Changes
  • Added the new feature for transcluding a value, rather than a post/key reference to it (which is still available). This comes in handy when you have a “normal” value and it’s a bit annoying to define a custom field just for that.

For example, let’s say that you have a template called mark-template.php with the following code:

{[ .mark-template /chili-web.php ]}

Then you could use it {[ =like this= | =cyan= mark-template.php ]} to get something {[ =like this= | =cyan= mark-template.php ]}

Download

This version is available for download from wordpress.org