When we relaunched one of our websites a couple of years ago we understood that a good syntax highlighter plugin would play a significant part moving forward. There's nothing more frustrating than visiting a website with line-wrapped code and poor syntax management... and since we tend to publish a lot of PHP and WordPress code, the choice of a syntax library wasn't a small one. This article will provide registered users with basic code that'll display highlighted text with a JavaScript-free WordPress shortcode function (using PHP's highlight_file() function).
Our former syntax highlighter plugin was overweight, slow, and it indiscriminately included JavaScript in the head for a dozen different languages that we rarely used. Additionally, we had to encode every snippet of PHP code to avoid formatting errors when rendered. This plugin-specific stupidity offered no backwards or lateral compatibility whatsoever... meaning that we were married to mediocrity until such time as we could move through every post and prepare the code for another product. That time came in March 2015 when we painstakingly moved through every published post on a number of websites over a sleepless couple of days when we corrected the encoded snippets to their natural unadulterated state, and into a format that was suitable for Crayon Syntax Highlighter .
Crayon Syntax Highlighter is brilliant. No question about it. However, we did want certain features that weren't available from Crayon... and knew that we'd end up having to do "something" in the future to better manage our snippets. There are a number of issues with Crayon - not the least of which is the speed: it's painfully slow. Our shortcode function mitigates the speed issue by bypassing the need for any JavaScript and caching the resulting snippet into a text file with Simple Cache.
WordPress Shortcode
The most notable issue with our shortcode is that it doesn't discriminate against PHP and other functions (yet). Some time back we shared an article that retrieved all PHP and WordPress functions into an array; the original purpose of that function was designed to determine where to best link our embedded function references. In the end it wasn't practical so we walked away from it. Know a solution? Let us know.
The shortcode of [php file="sample.txt"]
returns the following function (the referenced function was borrowed from this article).
Note the very intentional similarity to Crayon's default styling. This rationalization was intentional to ensure a seamless experience across our website.
You may return a chunk of code. For example, to print line 27 through 30 I'd use [php file="sample.txt" start="27" end="30"]
. For the purpose of a demonstration, I can choose to alter the $string
color by using [php file="sample.txt" start="27" end="30" string="#008000"]
(close to the Crayon default). The result:
If you're using the function as previously provided on Internoetics you'll note that the download link is missing. The retired download link would automatically zip the file and record statistical data before the file was returned to a user. Since then, we've implemented our own file manager.
Considerations
- Previously published on Internoetics as "Using PHP's highlight_file() Function in WordPress (with Automatic Zipping, Download Links, & Logging)". We've removed the features described in that title for simplicity.
- See also: "PHP's Syntax highlight_file() Function with Line Numbers and Alternating Coloured Rows" here.
- We continue to use Crayon but we use our shortcode highlghter when importing snippets from websites such as Snipplr.
Note, 3rd November, 2020: We now use this highlight code site-wide for reasons discussed in this article. There are still some issues but it is a superior solution since because we control the product, and it is lightning fast.
Download
To download this plugin you must be logged in. If you're following us on Facebook or you're subscribed to our mailing list we've sent you a download link directly.