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”;
$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]
Standing Tall » WordPress ãƒ—ãƒ©ã‚°ã‚¤ãƒ³ä¸€è¦§è¿½åŠ æ said,
July 15, 2007 @ 6:33 pm
[...] ordPress — tai @ 20:10 WordPress プラグイン一覧ã«ä»¥ä¸‹ã‚’è¿½åŠ ã€‚ ピーエイãƒãƒ‘イザー(PHPizer)é©åˆ‡ãªãƒžãƒ¼ã‚«ãƒ¼ã‚¿ã‚°ã§ãƒ©ãƒƒãƒ—ã•れ㟠PHP コーã [...]
There and Back Again » Wordpress PHP Syntax Highlighte said,
July 15, 2007 @ 6:33 pm
[...] l PHP — forestg @ 11:21 pm I don’t have a new theme yet, but I have a PHP Syntax Highlighter. It could use some more features, but i’m sure i’ll get to those [...]
.::PHPVietnam.net::. Open Source Developers in the Real Worl said,
July 15, 2007 @ 6:33 pm
[...] o trong số đó. Hôm nay, rảnh 10 phút, tiện dù thêm mấy cái plugin hay hay: Wordpress PHP Syntax Highlighter (tô mà u cho mã PHP) và Better handle <code> (cải tiến [...]
Netlex News said,
July 15, 2007 @ 6:33 pm
PHPizer Plugin
The PHPizer Plugin syntax-highlights PHP Code.
If you want to syntax-highlight a code block, just wrap the tags and (replace angle brackets with square brackets [php] PHP code [/php].
[php]
if($DESIGN==’blue’)
{
$bordure = ‘#000000′…
Weblog Tools Collection » Two Ells >> PHPizer Plugin said,
July 15, 2007 @ 6:33 pm
[...] “>Two Ells >> PHPizer Plugin Categories - WordPress Hack LinkyLoo — Mark Two Ells >> PHPizer Plugin: Syntax highlight PHP code that is posted on your WordPress blog. This co [...]
The Indiana Jones School of Management said,
July 15, 2007 @ 6:33 pm
PHPizer
Another day, another DLLH plugin: PHPizer. I need this for GFMorris.org somethin’ fierce.
[Hat tip to to Mark Ghosh's Weblog Tools Collection.]
wordlog.com » Yet Another PHP syntax highlighter - PHP said,
July 15, 2007 @ 6:33 pm
[...] ting about code highlighting plugins, so this time, it’s only gonna be a link to the PHPizer plugin.
Comments » RSS feed for commen [...]
Ozh said,
July 15, 2007 @ 6:33 pm
Seems nice.. What about the “pretty quotes” from wptexturize that keep you from cut&paste ?
[php]
$a=”hello”;
[/php]
Daryl said,
July 15, 2007 @ 6:33 pm
I don’t follow what you mean about pretty quotes keeping you from cutting and pasting
Ozh said,
July 15, 2007 @ 6:33 pm
Did you disable some wptexturize functions ? I see in your posts regular quotes (ie "e; ) but not the one WP usually creates ( ‘ ’ “ “ ) which are not working in scripts
Ingoal's Insight... » PHP-Highlighting added said,
July 15, 2007 @ 6:33 pm
[...] isn’t displayed all that nicely within posts. So I search a little and came up with PHPizer which is doing some nice php syntax highlighting now. Thumbs up to the author [...]
Amit Gupta said,
July 15, 2007 @ 6:33 pm
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.
Daryl said,
July 15, 2007 @ 6:33 pm
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.
.::PHPVietnam.net::. Buồn như con chuồn chuồn » said,
July 15, 2007 @ 6:33 pm
[...] trong số đó. Hôm nay, rảnh 10 phút, tiện dùng thêm mấy cái plugin hay hay: Wordpress PHP Syntax Highlighter (tô mà u cho mã PHP) và Better handle <code> (cải tiến [...]
SHRIKEE said,
July 15, 2007 @ 6:33 pm
Hi there. is there any way to keep the
SHRIKEE said,
July 15, 2007 @ 6:33 pm
erm… ok, that didnt work.
[php]
// i meant to ask how to prevent the
Marion said,
July 15, 2007 @ 6:33 pm
The best site, gretwork.
BestFreeSpace said,
July 15, 2007 @ 6:33 pm
BestFreeSpace…
Best Free Space For YOU blog….
Kendal said,
July 15, 2007 @ 6:33 pm
The best site with nice design.
toolvicarious said,
July 15, 2007 @ 6:33 pm
Are you still working on this plugin?
I get this error when I activate it
I am running Wordpress 1.5.1.2
What am I doing wrong?
Help me please!