Adding an Options Page

Almost every WordPress plugin involves some sort of option, and that means you'll need to create the form that lets users manage those options. If you went through the process of creating theme functions back in Chapter 7, the process for plugins will look very much the same.

To add an options page to the main navigation menu, you need two functions. One will display the content of the options page, and the other is a container for the add_options_page() function. Finally, you'll need to add your container function to the admin_menu() hook using add_action().

Listing 9-7 is taken from my Next Page plugin. I'll use this plugin as an example for the next several sections of this chapter, as it illustrates options pages, shortcodes, and template tags. Here, I've shown the bare minimum that's required to add an options page. In the next section, I'll show you how to fill in the form fields and process the input data into WordPress options.

Listing 9-7. An empty options page

// Add a new submenu under Options: add_action('admin_menu', 'next_page_add_pages');

function next_page_add_pages() {

add_options_page('Next Page', 'Next Page', 'manage_options', 'next-page',

// displays the options page content function next_page_options() { ?> <div class="wrap">

form method="post" id="next_page_form" action="options.php"> <h2>Next Page Options</h2>

<p class="submit">

<input type="submit" name="submit" class="button-primary" value="Update Options" /> </p>

} // end function next_page_options()

Unlike the widgets you created in the previous chapter, on the plugin options pages, you do have to define the form and its submit button. The form should always be posted to options. php. This file processes all the WordPress options. Once you have registered your plugin's options (which I'll show you in the next section), you can use just two lines to your form to process them.

The wrapper element (<div class="wrap">) is essential to the layout of all WordPress admin pages, so you must include it in your options forms. The form tags should go inside the wrapper. The id attribute is optional, but your form method should be post and the action should always be options.php (the file that processes all WordPress options).

The results of Listing 9-7 are shown in Figure 9-3.

Figure 9-3. The skeleton options page

Your form button styles should conform to WordPress standards. You should have one primary button (the one that saves the options, publishes the post, etc.) and it should be styled with the button-primary class. All other buttons should use the secondary class. These styles are consistent throughout the WordPress administration screens. The QuickPress Dashboard widget, shown in Figure 9-4, illustrates the contrast between the two button styles.





Save Draft Reset

Figure 9-4. The secondary (Save Draft, Reset) and primary (Publish) button styles

Now that you have an options page in place, it's time to add some fields to the form.

Blog Profits

Blog Profits

Discover The Steps To Blogging From Start Up To Making Money With your Blog, Starting Today. In This Guide You'll Find Out The Secrets To Making Money Online By Harnessing The Power of Blogging.

Get My Free Ebook

Post a comment