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!

8 thoughts on “Post Ender – A Simple WordPress Plugin Framework

  1. Hi TonyF!

    i really like your plugin, because it’s a good idea to have an option to add something to every post… like that you are on vacation or sick and and and 🙂

    but i have a big question:
    is there an option to insert text+functions or just functions like [fb-share-button] so that i can create the my own sharing experience?

    • I’m glad that you liked the plugin. It’s an interesting idea to be able to add functions and shortcodes inside the text. It seems to me that the shortcodes should work but I haven’t tried it yet. Being able to add other code and functions might be a little trickier. I’ll have to look into how that can be done.

  2. Great plugin – thank you! However, is there a way to have it appear BEFORE another plugin … I’m thinking specifically of a social sharing plugin. I’d love to be able to place something like “if you liked this post, please share it” in the PostEnder, followed by the social sharing icons. As it is now, that text appears AFTER the social icons.

    • The way the PostEnder plugin works is to add the text that you enter either at the beginning or the end (or both) of the existing text in a post. I’m not sure how it would be appearing after the social icons unless those icon were inserted into the body of the post somehow. I’d be curious to know what plugin you are using to add the social icons to your posts.

      • to help AHS & TonyF:
        i’m unsing “Digg Digg”, “VKontakte Share Button” and some other plugins! … and first comes the plugin “Digg Digg” and then the other one! so maybe wordpress is calling the plugins alphabeticaly! 🙂

      • PS: with “Digg Digg” you can call the social share buttons with a funktion like this: [fb-share-button]
        so maybe your AHS it would also be good to call functions via post-ender 🙂

Leave a Reply