PHPizer Plugin

From time to time, I’ll have occasion to include some PHP code in my entries, and PHP’s a lot easier to read if it’s syntax-highlighted. Many forums allow you to wrap special tags around you code to cause it to be syntax-highlighted, and though I’ve seen some WordPress plugins out there for doing syntax highlighting, they’ve seemed a little more complicated that has seemed quite necessary, or they’ve been dependent upon some library or another being in place. So I rolled my own, which can be downloaded here.

If you want to syntax-highlight a code block, just wrap the tags <php> and </php> (replace angle brackets with square brackets) around it and this plugin filters it through the highlight_string() function. You can also specify a div class name that allows you to define a style for the container div. I’ve chosen to set my code off by putting it in a gray box with some margin and padding and a thin border. The default class name is set to “code”. The code itself for this plugin (to demonstrate the plugin in action, which strikes me as being rather like standing between two mirrors and seeing infinity as they reflect one another’s images recursively) is as follows (note that I had to screw with the code just a smidgin to keep it from parsing the php marker tags in the regular expressions — if you want to use this, download the source from the link, as that’s unscrewed-with, commented code):

[php]
$phpizer_div_class=”code”;

function phpizer($text) {
global $phpizer_div_class;
$parse=0;
$codebuffer=””;
$finaltext=””;
$count=1;

//Get lines into an array so we can iterate over them.
$lines=split(“n”,stripslashes($text));

foreach($lines as $theline){
//If we’re starting a code block, set flag and suppress marker tag display.
if(preg_match(“/[ php]/”,$theline)){
$parse=1;
$theline=””;
}
if(preg_match(“/[ /php]/”,$theline)){
$parse=0;
$finaltext .= “

n” . highlight_string(“n”,1) . “

n”;
$count++;
$codebuffer=””;
$theline=”n”;
}
//Not code, so just add current line.
if($parse==0){
$finaltext .= $theline;
}
else{
$codebuffer .= $theline;
}
}
return $finaltext;
}

add_filter(‘the_content’, ‘phpizer’, 8);
add_filter(‘comment_text’, ‘phpizer’, 8);
[/php]

22 thoughts on “PHPizer Plugin

  1. Did you disable some wptexturize functions ? I see in your posts regular quotes (ie &quote; ) but not the one WP usually creates ( ‘ ’ “ “ ) which are not working in scripts

  2. You should disable wptexturise() in the posts that contain code so that the PHP code in your posts is copy-paste friendly. If you copy the code from your post & paste it in a text editor, you’ll notice the difference. The quotes change from “” to “”.
    Also, adding line numbering to your code would be beneficial. Creating a hack/plugin/project is easier, but keeping its development continous is quite hard. 🙂

    I think that you should check out my plugin iG:Syntax Hiliter. It scores quite a lot over PHPizer. 😉

  3. Thanks for the feedback. I suppose the value of a plugin is really dependent upon what you want out of it. I wanted a simple, concise plugin that would perform the very simple task of rendering PHP code nicely on the screen. Mine works pretty well for that purpose, and I’m happy with it, though of course improvements can always be made to any piece of code. Anybody wanting more functionality than what my plugin provides should definitely look into your plugin, which certainly seems to be much more fully-featured.

  4. Spring is here and summer is just around the corner, and in order to ward off the sun’s damaging rays, as well as to look cool, it’s decidedly sunglasses season. But with so many options to choose from, what to wear? Ray-Ban Wayfarers, that’s what, iconic eyewear since 1952 and after a few sluggish years (or decades) still as fashionable today as they were over 50 years ago. oakley dangerous sunglasses brown

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s