and like us on FB

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).

1
<?php
2
/*
3
    Count Number of LinkedIn Shares With WordPress Shortcode
4
    http://www.beliefmedia.com/linkedin-shares
5
*/
6
 
7
 
8
function beliefmedia_linkedin_shares($atts) {
9
 
10
  $atts = shortcode_atts(array(
11
    'url' => '',
12
    'n' => 1,
13
    'cache' => 3600 * 12
14
  ), $atts);
15
 
16
 global $post;
17
 if ($atts['url'] == '') $atts['url'] = get_permalink($post->ID);
18
 
19
 $transient = 'bmlk_' . md5(serialize($atts));
20
 $cachedresult =  get_transient($transient);
21
 
22
 if ($cachedresult !== false ) {
23
  return $cachedresult;
24
 
25
  } else {
26
 
27
   /* Get LinkedIn Data */
28
   $json = file_get_contents('http://www.linkedin.com/countserv/count/share?url=' . $atts['url'] . '&format=json');
29
   if ($json !== false) $data = json_decode($json, true);
30
   $return = (!empty($data)) ? intval($data['count']) : 'Unavailable';
31
 
32
   /* Number format */
33
   if ($n) $return = number_format($return);
34
 
35
  set_transient($transient, $return, $atts['cache']);
36
  return $return;
37
 }
38
}
39
add_shortcode('linkedinshares', 'beliefmedia_linkedin_shares');

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:

27
   /* Get LinkedIn Data */
28
   $json = file_get_contents('http://www.linkedin.com/countserv/count/share?url=' . $atts['url'] . '&format=json');
29
   if ($json !== false) $data = json_decode($json, true);
30
   $return = (!empty($data)) ? intval($data['count']) : 'Unavailable';

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.

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.

Shortt URL for this post: http://shor.tt/14ny