Last week I explained how to take a function and turn it into a plugin for your WordPress site. The plugin was fairly basic, just a function that created a list of social bookmarks. To use it you would just make a call to the function within your template file. However, we can go one better and give the user an option of either controlling where the bookmarks are displayed (as it currently is), or for those less confident in editing their template files, we can give them the option to choose to simply have the bookmarks added to the bottom of their post.
So to do this, first we need to create an options page. To add an options page we need to use an action which calls a function to add the page to the options menu, plus it tells that menu option which function to run for the options page (with me so far?!). So step 1, call the action
You may noticed that I’ve omitted the last parameter. It’s not essential unless you want to give your plugin a menu icon that appears to the left of the label in the menu. The fourth parameter tells the function that the options page function is in the same file.
So now we’ve got the menu label and on clicking it, it will run the function sblinks_options(), so we need to set our page up within that. For a function page you need your processing script and then the page/form below it, wrapped within the same function. So for our page for our plugin we just need to give the option of whether to attach the output to the bottom of every page and post or not.
For our HTML we can use:
This outputs a simple form with a checkbox. The markup and classes used for the divs are already used in the WordPress admin so this will keep the page in the same design.
Now we need the code to process this form. As this is an option then we save it in the options table within the WordPress database. To do this we use the function update_option, ie.
Here we’ve checked to see if the form has been submitted. Then we use the update_option to update the option value ‘sboption’ with the value of the checkbox (0 for unchecked, 1 for checked). With the update_option() function, it checks to see if the option specified exists. If it doesn’t then it will add the option, if it does exist it will update the existing one, so it does two jobs in one. Once we’ve updated the option we can echo out the message that the selection has been saved. Again, the markup used fits in with WordPress’s own markup and will allow the message to fade in as it does on other pages.
So now we’ve got the form and the form processing script. Finally we need to check what the option currently is in the options table, so that the checkbox can reflect that. To do this we use the get_option() function to get the value of the option, ie.
As we’ve stored the option value as 0 or 1 then we can use a simple if statement to check the value of it. 0 will also give a false value, so if the value is 1 (ie. true) then our variable $sboption gets an additional piece of HTML markup to add to the form, making our checkbox checked. If the value is false then it’s just set to be empty. We can then insert this into our input code:
[sourcecode language=”html”] />[/sourcecode]
This will then set the checkbox as checked if the option has been selected previously.
This can go into the plugin file that we created last week.
Next week I’ll conclude this plugin to show you how to alter last weeks function to check on the option and output the social bookmark links accordingly.
About the Author
A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.