Post Ender – A Simple WordPress Plugin Framework

Update: When trying to add the Post Footer plugin the WordPress repository I discovered that name was already taken. So Post Footer has now been renamed Post Ender.

Post Ender is now available  at in the WP.org plugin repository

As they say, necessity is the mother of invention. In one of our recent meetings for the SportsDashboards project it was decided that we wanted to add some text at the bottom of each post on the Sports Stories blog with information about SportsDashboards. I knew there I could probably find a plugin to do this out in WordPress land but it seemed easy enough and I’ve been wanting to write a plugin for a while now. So I set out to do just that.

More than just write a plugin, I wanted to create a simple framework for creating other plugins. Nothing too fancy, just a class-based structure that I could use for any plugins I wanted to write. I’ve wanted to do this project for quite a while now but never got around to it. Now I had a real task that needed to be done so it was time to tackle plugins. I’ve looked around at a variety of plugins to see how they were written and there didn’t seem to be any standard way of structuring a plugin. Most seem to be just a bunch of PHP functions in a file. Functional, but not very pretty or maintainable in anything but a very simple plugin. The plugin I need would be a very simple one so I could have just put together a few PHP functions and saved myself a lot of time but I this was the opportunity to create the framework I was thinking about and to learn more about writing plugins.

I did do a search for WP plugin frameworks and found a few interesting ones but they all seemed to add more bells and whistles than I wanted. So the Post Footer plugin was born. Post Footer does one thing. It adds text to the bottom of every post. Oh, I did add a few lines of code for a [post_footer] shortcode in case someone wanted to add the text to individual posts.

The main idea is to separate the code into sections: common code, admin code, and front-end code. I saw this technique in another plug that I ran across but I unfortunately can’t remember which one  it is to be able to give credit where credit is due. The main file looks like this:

if ( !class_exists("PostFooter") ) {
    class PostFooter {

        function PostFooter() {
            <-- constructor code goes here -->
        } end PostFooter

        function init() {
            if ( is_admin () ) {
                // Load the admin page code
                if ( @include ( dirname ( __FILE__ ) . '/inc/admin.php' ) ) {
                    $PostFooterAdmin = new PostFooterAdmin ();
                } else {
                    PostFooter::deactivate_and_die ( dirname ( __FILE__ ) . '/inc/admin.php' );
                }
            } else {
                // Load the frontend code
                if ( @include ( dirname ( __FILE__ ) . '/inc/frontend.php' ) ) {
                    $PostFooterFrontend = new PostFooterFrontend ();
                } else {
                    PostFooter::deactivate_and_die ( dirname ( __FILE__ ) . '/inc/frontend.php' );
                }
            }
        } // end init

    } // end PostFooter class

}

As you can see there is logic in the init function to look at how the plugin is being called and load either the code to display the admin page or the front-end code for adding the text to the posts. The options page, which is mostly HTML is also broken out into a separate file to make it easier to maintain.

The code to add the text to each post was made easier by similar code in the book “Smashing WordPress: Beyond the Blog” by Thord Daniel Hedengren. The plugin example in the book shows code to add text at the end on each post using a template tag. It was pretty simple to change that to either use a shortcode tag or an option on the options page.

So there you have the Post Footer plugin. It was fun to write and hopefully it might be a helpful example to someone else trying to write there first plugin. You can find more information and download the code here. Sometime soon I hope to have this pushed out to the WordPress.org plugins directory so you can install it directly from there.

Happy Coding!

Speaker Repair Project

Many years ago I picked up a pair of speakers at a flea market. I don’t remember how much they were but since I probably didn’t have much money back then I’m guessing they had to be under $10. They were good solid cabinets but the drivers were way beyond repair. This seemed like a fun project and a cheap way to get  some new speakers so I proceeded to pickup new 8″ woofers and a couple of piezo tweeters from Radio Shack (back when they still used to sell all kinds of cool electronic parts). I swapped out the old drivers with the new and had a fairly nice sounding pair of speakers. They were my main speakers for quite a while and finally got moved to my surround speakers (replaced with a pair of Bose AM5’s which I’d really like to retire). They’ve severed me well for the past 25 years or so.

A couple of months ago I started hearing strange buzzing noises coming from the surround speakers while watching movies. A few weeks ago I finally got around to investigating the noise and found the foam surrounds on the woofers had completely disintegrated, hence the unsavory sounds. Time to replace the drivers again. I had recently been introduced to Parts Express which has a great selection of parts for building (or re-building) speakers. I found some likely replacement candidates for the existing drivers. I decided to replace the tweeters as well even though they were still working well. I pulled out the old Radio Shack drivers, put in the new ones and once again I have a pretty good sounding pair of speakers. Ready for another 25 years of use.

At some point in the not to distant future I’d like to replace all the speakers in our living room. I’m currently considering the Intimus speakers from Aperion Audio. These look like they’re solidly build (kind of like my flea market boxes), the price is right and they’ve gotten very good reviews. At that point I’m thinking I’ll move the newly re-built speakers into my office where they can live on.