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.
Bitcloud asked me to post my plugin “as-is”. Glad to help, but just remember–this is my first attempt at writing a plugin. Back up your blog before installing!
wp-content/plugins
directory.MY_FOOTER_TEXT
definition with the text you want to appear in your blog’s footer.Pingback: SonicChicken weblog » Blog Archive » Displaying (vs executing) PHP code on web pages
Pingback: SonicChicken weblog » Blog Archive » New website for my wife’s business
I’ve posted some more information about Statcounter. I put a link to this post in that article, so I should have gotten a pingback here, but didn’t for some reason. Nothing awaiting moderation, either. Strange…
Awesome Plugin, thankyou makes changing themes so much easier.
Would it be possible to also be able to use this for php in the
footer as well? If so how would I do that.
I think you should be able to embed PHP. The only problem would be quoting, since the footer text is single-quoted already. If the PHP you want in the footer is simple (i.e. can be embedded in a quoted string), just go for it. If the PHP isn’t simple, write it as a function and then insert a call to the function in the quoted string.