While working on an earlier post, I needed to find a way to display a PHP module on a web page without having it executed. I knew this was fairly simple and involved the .phps file extension, but I hadn’t done it before. A quick search of the PHP site pointed me to the highlight_file function. About halfway down the page there is a blurb about the MIME type that I needed. It turns out that my web hosting service already has this enabled, so all I needed to do was get a .phps extension on my file. I did it the easy way, with a softlink:
ln -s myfile.php myfile.phps
For those who don’t know Unix, this effectively creates another name for the same file, which is exactly what I want. This leaves the original .php alone, so that it can be run by the web server. But is also allows me to post the code on the web, using the .phps “file” which the web server will display instead of executing. The elegance of this solution is that if/when I modify the original code, I won’t have to make any changes to the displayable code because there is really only one file (with two distinct names).
A few days ago, Joseph and I were talking about bolting statcounter onto our blogs. He’s the one that pointed out statcounter to me, and all he needed to do was add the statcounter script to his theme’s footer.php file. This would probably work for any single-theme site, but I’m still running multiple themes on my blog. I need to do one of the following if I want to use statcounter:
- Drop all but one theme. There are several good reasons for this, most important of which is maintenance. This would allow me to add the statcounter script to the remaining theme’s footer.php and I’d be done.
- Add statcounter to all my themes. This probably wouldn’t be too bad, except for maintenance–whenever a new version of one of my themes came out, I’d have to manually reapply my changes.
- Write a plugin to insert the statcounter script in the footer of every page. This would be the most work up front, but it would give me a good excuse to learn how to write plugins for WordPress.
I found some useful resources for plugin authoring. The WordPress codex has a section on writing plugins. Owen’s tutorial is a great introduction–just read it and start hacking!
Carthik’s Plunge into Plugins article has lots of good advice, but isn’t a tutorial–check it out after/while you get started with Owen’s page. More good info is available on the Codex page Writing a Plugin.
I’ve started working on this plugin, and I’ve already been bitten by the “extra blank line” problem. (Admin interface was reporting “Cannot modify header information - headers already sent by…” error. Note to self–scroll to the bottom of each PHP file, and make sure the PHP close tag is right at the bottom of the file.) I have the code to insert arbitrary text into the footer, but I still need to add the Options menu which would allow J. Random User to edit the text to be inserted. (Right now, the text is hard-coded in the “plugin”, which I have installed and activated on this blog. Check the bottom left corner of any blog page for the statcounter.)
I don’t see any reason for this plugin to be statcounter-specific. It would be more useful to provide some sort of generic footer plugin which would allow HTML or javascript to be inserted in the footer. We’ll see how it goes. If I’m happy with it by the end of the night or later this week, I will go ahead and release it.