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!