My First Wordpress Plugin and Widget
I knew this day would come. I finally bit the bullet and decided to figure out how to create a Wordpress Plugin and Wordpress Widget. I did it as part of a new members-only web site that I’m creating and wanted to provide a way for members to easily display my data on their Wordpress blogs.
Like most things, once I got everything working it really doesn’t seem too difficult, but getting to that point was a little frustrating. I didn’t find very much in the way of good documentation and examples, so I just looked at a few of the plugins and widgets that I already use on some of my blogs and tried to figure out what they were doing.
The functionality of the plugin was pretty simple. I wanted to make it possible for members of my new site to be able to add some special syntax in a post and get info from a special RSS feed to be display. In order for the feed to display correctly the plugin requires a little configuration after the user has activated it. That meant I needed to create an "options" page. So, I needed to create a few functions and add some calls to the "add_action" function:
- add_action(’the_content’, ‘myPluginFunction’); - this call registers the function (myPluginFunction) I created that will get called when a page or post is displayed (that’s what the "the_content" means, it is one of the plugin actions you can register for)
- add_action(’admin_menu’, ‘myPluginSetup’); - this causes the myPluginSetup function I created to get called when Wordpress displays the admin menus. In the myPluginSetup function I created I just have a call to "add_options_page" to register a function that will display my custom option page that allows users to configure my plugin.
- The function I created to display my option page was pretty simple and there was decent documentation on the Wordpress developer site.
The widget was a little harder to get working because I wanted to allow the user to do a little configuration once they add it to the sidebar. Because I was trying to keep things pretty simple though, I was able to use the Wordpress functions of get_option and update_option for retrieving and story my options. This meant that I didn’t have to create a custom table and do any sql statements (not that I think that’s difficult, but it was just one more thing I didn’t want to have to deal with).
I still have some fine tuning to do, but I do feel pretty good about having gotten things to basically work. Now that I have one under my belt I just may have to go create some more!
