Laying the Groundwork

To start off, we'll first create a new file in our library directory called options.php. This is where our custom options form and related functions will live. Before doing anything else, let's include it in functions.php:

chapter_07/v1/wicked/functions.php (excerpt)

// include theme options include('library/options.php');

We'll begin by setting a few theme-specific variables that we'll refer to later on. This is primarily to make it easier for you to adapt the code to your own theme:

chapter_07/v1/wicked/library/options.php (excerpt)

// Set some theme specific variables for the options panel $childthemename = "Wicked Theme"; $childshortname = "wt"; $childoptions = array();

$childthemename is, of course, your theme's name. We'll use this variable any time we want to display the theme's name in a link or on a page in the admin panel. Because it's a name, and will be the same in every language, no localization function is required.

$childshortname is an abbreviated version of your theme's name. We'll use this so that none of the settings or data we store conflicts with any other similarly named options created by WordPress, or any plugins that may be in use. We'll append it to variables like $shortname . M_text_color".

$childoptions is where the options themselves will be stored. For the moment it's an empty array, but we'll be filling it up shortly.

Next we're going to create a function called childtheme_options, which will define our options and how they'll be displayed in the back-end administration interface. Inside that function we have two tasks to accomplish: we'll pull in our variables using the global keyword, and then we'll fill up the $childoptions array with all the information required for our options.

Let's start with the easier task:

chapter_07/v1/wicked/library/options.php (excerpt)

function childtheme_options() {

global $childthemename, $childshortname, $childoptions;

Good! Now we have access to our variables. Out of Range

The concept of variable scope in PHP is beyond the, uh, scope of this book. Since this is the only place we'll be using it, just include that line in your function and you'll be fine.

Next we need to define all our options. $childoptions is an array that contains a bunch of other arrays: one for each option you want your theme to have. Each of those arrays, in turn, should have the following values in it:

name the name of the option desc holds the description of the option along with any instructions for using it; the name and description will both be displayed on your options panel id used to save and update the option in the database; we'll use the $childshortname variable here so that our field will be unique

Was this article helpful?

0 0

Post a comment