WP resource: Quick & Dirty PHPSource Printer
- resource:
- Q&D PHPSource Printer
- version:
- R1.1.1
- download:
- sourceprt.zip
- source:
- source.php
Before going public with the return of guff, I played in WordPress for a few weeks offline, to get comfortable and decide which features I wanted to put to use. During that time I worked over and reworked some WordPress plugins. It’s how I like to get into trouble. And I rolled a few plugins of my own. Not examples of brilliant PHP programming mind you, but they work well enough that I’ve implemented them on guff. I’ll be releasing the latest one to the wild (the first is a simple random “quote” utility, and I see no need for yet one more of those under a GPL).
As I thought over how best to present my plugin, I decided I wanted the source on-site in prettified syntax highlighting for that cool factor, as well as stashed away in a zip file for easy download. In regards to the former, one option is to link to it as a PHP Source file (phps). Sadly, the server guff sits on isn’t configured for it. Yeah, I’m bugging my provider, Ok? Another method is yet another plugin that can automagically generate syntax highlighting for me within a post. But if I want to output the code so one can actually read and copy it, I chance playing havoc with my layout, and that’s not a Good Thing™. Even solving that, posting code in a blog post is best left for code fragments, and not several hundred lines of an entire script. That deserves it’s own uncluttered page.
So how to get what I’m looking for, but avoid the extra work of rewriting PHP scripts as colorized HTML documents? Well, I could give myself extra work and create something that will do it for me. And that’s sort of what I did!
Ladies and gentlemen, meet the Quick & Dirty PHPSource Printer, which reads and prints out PHP files with syntax highlighting. My first publically released PHP script for WordPress—and it’s not even a plugin. I’m sure I’m not the first to cobble something like this together, but the coding was easy since I make use of built-in PHP functions for syntax highlighting. It would have taken longer just searching around for a script that does the same thing!
To use it just upload the PHP file (source.php) to your WordPress home directory. To have it do its magic on the source of a plugin, link to it by providing a query of the plugin’s filename:
/source.php?file=random-item.php
That’s it. By the way, random-item.php is the quote generator I mentioned earlier… There’s one variable in the script for specifying a path to the file (which is presently pointing at my plugins directory). Also, an additional query parameter (’ln’) outputs line numbers:
/source.php?file=random-item.php&ln
Pretty sweet. And I didn’t sweat over the keyboard to get it out. I always like that. (The line numbering functionality is someone else’s work. I’m not completely against seeking out scripts from other people, especially when it speeds up coding time.)
I note in the script’s comments that something like this can potentially read any file on a web site, so it’s important to limit the directory and files it can access through the variable assigning the directory path (by default this is ‘wp-content/plugins/’). So heed my warning: don’t set it to your root directory and let people dig around for your config files!
Author: Kaf Oseo
Categories: Technical Folly · WordPress
Comments: (0) · Leave a comment · Trackback URL