Just another 3DN Site

Post thumbnail of Adding an Expertise Section to Wordpress

Adding an Expertise Section to Wordpress

Adding an Expertise Section to Wordpress

I’ve been thinking about a way to get people to add links to our blog on their site. A friendly way to do this is by adding an ‘expertise’ section to the site where people can go after they sign up. Here people can ask questions which will be emailed to the section maintainer(s) so they can answer any questions involving their expertise as timely as possible.

This is not a standard Wordpress functionality however so I’m going to write my own Wordpress plugin for this.

Requirements

I first created an ‘Expertise’ page to the blog. This page is freely accessible without having to log in. Then I created some pages with the Expertise page as it’s parent page. We need to have the ‘Member Access‘ plugin installed on the blog in order to make the separate expertises member-only. The first three expertise pages I set up were for ‘Linux/Unix’ (maintainers: FredLeeflang, TigerP), ‘Programming’ (maintainers: FredLeeflang, TigerP) and ‘IPv6′ (maintainers: TigerP).

I manually added the ‘maintainers’ custom field to each of the separate expertise pages to get started. When the plugin is done this field should be added automatically.

Getting Started with the Plugin

Creating a plugin for Wordpress is easy. Simply go inside the wp-contents/plugin directory, create a new directory there and some files and you’re on your merry way. I also took some effort to import the directory into my public subversion server:

svn import experts https://secure.dutchie.org:444/svn/trunk/experts

So  you should also be able to check out the source while I’m developing it by:

cd wp-contents/plugins
svn co https://secure.dutchie.org:444/svn/trunk/experts

So all I did to get the plugin to show up with some meaningfull information was to add a file experts.php and a readme.txt. The expert.php has a header in it:

/*
Plugin Name:    Experts
Plugin URI:     http://wordpress.3dn.nl/experts/
Description:    This plugin allows you to add experts to several pages on your site. Experts will get notified when a comment on the page is posted, allowing them to quickly respond even though they don't check the site every day.
Author:         Fred Leeflang
Version:        0.1
Author URI:     http://wordpress.3dn.nl/

Setup:
 1) Install the plugin.
 2) Go to the admin menus, and in the "Options" panel, select "Expers".
 4) Configure the plugin if you need.

*/

Which by itself is sufficient to make the plugin show up like this in the admin -> Plugins section:

Obviously this doesn’t make the plugin do anything yet but it just goes to show that having a header in a file makes the plugin show up with the correct description etc. in the plugin admin screen, ready to be activated!

Got Admin Options?

First we want to make a basic admin page. This is easily done with:

add_action('admin_menu', 'expert_ap');

function expert_ap() {
 //create new submenu under 'Settings'
 add_submenu_page('options-general.php', 'Expertise Settings','Expertise Settings', 'administrator', __FILE__, 'expert_settings_page');

 //call register settings function
 add_action( 'admin_init', 'register_expertsettings' );
}

function register_expertsettings() {
 //register our settings
 register_setting( 'experts-settings-group', 'onpost', 'intval' );
}

This is not complete yet but this shows two important mechanism; adding a submenu to an existing (eg. ‘Settings’) menu and registering settings for our plugin.

The add_submenu_page() function links our settings menu under Wordpress’ settings (through using options-general.php), gives it a label, sets which capability is required and sets which PHP function must be called when the user selects the menu (expert_settings_page() which we have not added yet).

The add_action() function adds the function register_expertsettings() to the admin_init section of the page. There’s actually some pretty tricky stuff going on in register_expertsettings(), people who have coded plugins ‘the good old way’ will find out what I mean later. Essentially the register_setting() call adds a variable and a variable validation to a pulldown we’ll design later on. There will be no $_POST processing, HTML filtering etc, Wordpress will do all that for us. We just need to make sure that the form POST’s to options.php and make sure that the variables we want to get from the form get registered with register_setting().

The Form

Because of letting Wordpress handle our variables, we only need to build the form itself in expert_settings_page().

function expert_settings_page() {
?>
<div>
<h2>Experts</h2>

<form method="post" action="options.php">
 <?php settings_fields( 'experts-settings-group' ); ?>
 <table>
 <tr valign="top">
 <th scope="row">On Post in Expertise Section</th>
 <td>
 <select name='onpost'>
 <option value="1">Send Email</option>
 <option value="0">Do Nothing</option>
 </select>
 </td>
 </tr>

 </table>

 <p>
 <input type="submit" value="<?php _e('Save Changes') ?>" />
 </p>

</form>
</div>
<?php } ?>

Sample Expertise Area

I’ve set up a sample expertise area for Wordpress Expertise. While I’m developing this plugin further I’ll post some more followup in there and probably later on I’ll add one or more articles about the development so stay tuned!

Read More

If you enjoyed reading this article you might also like other articles in the Coding category:

Or in the Wordpress category:

Both older and newer articles in the Coding category will be listed here. Should you need (free!) Wordpress Expertise, please feel free to check out our Wordpress Expertise area.

Posted by fred leeflang, 15 February 2010 at 08:16
Leave a Comment

Threaded commenting powered by Spectacu.la code.