PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Creating a WordPress Plugin II

Posted by on 22nd Mar 2009 | 2 comments

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

[sourcecode language="php"]add_action(‘admin_menu’, ‘add_options_pg’);[/sourcecode]

Second step is to create the function ‘add_options_pg’ which adds the menu option:

[sourcecode language="php"]function add_options_pg() {
add_options_page(Page Title, Menu Title, user level / capability, file, function name, icon url);
}[/sourcecode]

The above function takes the given parameters to create the menu label. For our plugin we could use

[sourcecode language="php"]function add_options_pg() {
add_options_page(‘Add Social Bookmarks’, ‘Social Bookmarks’, 8, basename(__FILE__), ‘sblinks_options’);
}[/sourcecode]

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:

[sourcecode language="html"]

Manage Social Bookmarks

Social Bookmark Options


[/sourcecode]

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.

[sourcecode language="php"]if (isset($_POST['sb_submit'])):
update_option(‘sboption’, $_POST['sboption']);

echo ‘

Option Saved!

‘;
endif;[/sourcecode]

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.

[sourcecode language="php"]if (get_option(‘sboption’)) {
$sboption = ‘ checked=”checked”‘;
} else {
$sboption = ”;
}[/sourcecode]

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 now gives us the final code as:

[sourcecode language="php"]< ?php
add_action('admin_menu', 'add_options_pg');

function add_options_pg() {
add_options_page('Add Social Bookmarks', 'Social Bookmarks', 8, basename(__FILE__), 'sblinks_options');
}

function sblinks_options() {
if (isset($_POST['sb_submit'])):
update_option('sboption', $_POST['sboption']);

echo '

Option Saved!

‘;
endif;

if (get_option(‘sboption’)) {
$sboption = ‘ checked=”checked”‘;
} else {
$sboption = ”;
}
?>

Manage Social Bookmarks

Social Bookmark Options


< ?php } ?>[/sourcecode]

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.


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.

2 comments - Leave a reply
  • Posted by Paul on 22nd Mar 2009

    This is a terrific follow up to the first part and is turning into an amazingly useful series of blog posts. Thanks Sarah, you've gotten me very interested in learning more about plugin development now. Off to Amazon for that new book on the topic…

  • Posted by Sarah on 29th Mar 2009

    Hey Paul, glad you're enjoying the posts. Final part this week on wrapping up the plugin :)