Skip to content

Pravin Paratey

Natural Language Processing, Data mining and Information Extraction consultant based in London.

Mar 28 2009
Mar 28 2009

Hacking wp-syntax plugin to show header

I was recently asked how I got the wp-syntax plugin to show a header like so:

test.cpp
1 int main() { 2 return 0; 3 }

To show the test.cpp file name, I modified the wp-syntax.php file (present in /wp-content/plugins/wp-syntax/) like so:

Changed the regular expression in the wp_syntax_before_filter function from:

wp-syntax.php
function wp_syntax_before_filter($content) { return preg_replace_callback( "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|\s)+>(.*)<\/pre>\s*/siU", "wp_syntax_substitute", $content ); }

to

wp-syntax.php
function wp_syntax_before_filter($content) { return preg_replace_callback( "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|header=[\"']([\w-\. ]*)[\"']|\s)+>(.*)<\/pre>\s*/siU", "wp_syntax_substitute", $content ); }

And the wp_syntax_highlight function to:

 94 function wp_syntax_highlight($match)
 95 {
 96     global $wp_syntax_matches;
 97 
 98     $i = intval($match[1]);
 99     $match = $wp_syntax_matches[$i];
100 
101     $language = strtolower(trim($match[1]));
102     $line = trim($match[2]);
103     $escaped = trim($match[3]);
104     $header = trim($match[4]);
105     $code = wp_syntax_code_trim($match[5]);
106     if ($escaped == "true") $code = htmlspecialchars_decode($code);
107 
108     $geshi = new GeSHi($code, $language);
109     $geshi->enable_keyword_links(false);
110     do_action_ref_array('wp_syntax_init_geshi', array(&$geshi));
111 
112     $output = "\n<div class=\"wp_syntax\">";
113 
114     if($header) {
115         $output .= "<div class=\"wp_syn_hdr\">" . $header . "</div>";
116     }

Node the addition of lines 104 and 114-116

All you have to do is add another attribute header="header-text" in your pre tag. ex. <pre lang="php" line="1" header="wp-syntax.php">

Latest Articles