Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. WordPress

How to Create a Better WordPress Options Panel

Read Time: 15 mins

Today, we'll go through the entire process of creating an admin options panel for a WordPress theme, using the excellent WooFramework as an example. Then, we'll take things a step further, as we implement jQuery to improve some of the functionality.

Tutorial Details

  • Program: WordPress
  • Version: 2.7, 2.8, 2.9 onwards
  • Difficulty: Intermediate
  • Estimated Completion Time: 1.5 hours
Final ProductFinal ProductFinal Product

WordPress is one of the most popular Content Management Software (CMS) systems out there. Whether it be for a client project or for selling themes on ThemeForest, WordPress is fast emerging as a CMS of choice for many web developers. It's relatively easy to use, but can be made even simpler when you include an administration panel for users. Rather than having to open up the PHP template files and fiddling with the code, users can directly use the options panel to interact with your WordPress theme.

For example - if your theme has red, blue and green color schemes, and each has a corresponding CSS file, it would be far easier for a user to select their preferred color from a dropdown list. So today, let me guide you through the entire process of creating and enhancing a WordPress admin panel page inspired by Woo.

Step 1

Before we begin creating the admin panel, we need to have a theme, right? So download the source files provided with the tutorial. I've slightly modified the Classic WordPress theme. Place the 'nettuts' folder (I've named the theme 'Nettuts') in your wp-content/themes folder. You should see the following files:

  • functions.php (blank)
  • index.php
  • comments.php
  • footer.php
  • header.php
  • rtl.php
  • sidebar.php
  • style.css
  • screenshot.png
  • An images folder with two files

Most of our work is going to be done within functions.php file.

A theme can optionally use a functions file, which resides in the theme subdirectory and is named functions.php. This file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages).

Suggested uses for this file:

  • Define functions used in several template files of your theme
  • Set up an admin screen, giving users options for colors, styles, and other aspects of your theme

(From the WP Codex)

Step 2

Now that we've got our WordPress theme set up, go to Appearance>Themes, and activate the nettuts theme.

Activated? Ok, great. Now we have to think of a layout for our admin panel page. Here's the structure I've decided upon:

Let me explain all of that to you. The options set is going to be wrapped up into a div named "rm_wrap" and then "rm_opts" for the options. Then we start a form, with all the inputs within it. Each section of options(general Settings, Homepage settings, Blog settings etc) has a separate div with a class of "rm_section". This div has a title (for the name) as well as several input divs within it. By using classes like <div class="rm_input rm_select">, we can style dropdowns, text inputs, and textareas differently.

Now, the most important thing is that the coding of this shouldn't be done manually - we should use PHP's flexibility as much as possible. That means efficiency: don't code manually when you have loops for you!

Step 3

Begin by opening up functions.php in your favorite code editor (I use Notepad++). Enter the following code:

The two PHP variables hold the name of your theme (Nettuts in our case), and a shortname which you've defined (nt in our case). The shortname is used to prefix all our theme options names, and is usually unique to a particular theme. Moving on, we'll write some code to automatically generate a list of WordPress categories, rather than having users type in ID numbers. Enter the code below under the already typed code:

This snippet uses WordPress' built-in get_categories function to fetch all categories, and then uses a foreach loop to store them in the variable $wp_cats. The options "Choose a category" is then added to the top of the array

Step 4

Now we start entering a list of options for the theme. See below, and paste it into your functions.php file:

That was a large chunk of code, which surely warrants some explanation. So here we go:

  • The PHP variable $options stores the entire list of options for the theme.
  • It is composed of a number of arrays, each with a "type" key to signify how it will be displayed, and what it does.
  • We start with a "type" => "title" array - this will be used to show the themename and a title at the top of the page
  • Each section (General, Homepage and Footer) has a separate list of options.
  • We start a new section by closing any previous sections, declaring a new section using array( "name" => "Footer",
    "type" => "section")
    and opening a new section.
  • Each option can have the options specified below:
    name: The name of the input field.
    desc: A short description explaining what it is to the user.
    id: the id of the field, prefixed by the shortname. It will be used to store as well as access the options.
    type: the input type - select, text or textarea
    options: used to declare an array of options for a select type input.
    std: the default input value, used if no other input is given.

Step 5

Try navigating to WordPress. You will see that there's no option anywhere to actually view the admin panel page; so how can we view it? Add the following pieces of code to the functions.php file:

This function is meant for updating options, as well as adding a menu page. If the options are being saved (indicated by a hidden variable save), then all the options are updated with their new values. If the options are being reset (indicated by another hidden variable with a value reset), then all of the options are deleted. The last line adds a menu page - the parameters are respectively, name and title, the user authorization level required to view the page, the save page and the function used for display/saving (called mytheme_admin in our case). See the mytheme_add_init, a blanbk function? Let that be, we'll come to it later.

Step 6

Still no theme options page, right? Well, remember the mytheme_admim function we had talked about a few lines ago? We still haven't written that function. So use the code from steps 6,7 and 8 to write that function. Starting off:

Pretty simple, right? If the options have been saved, write a message indicating so. Likewise for resets. You'll notice a class="updated fade" - WordPress will automatically fade this out in a few sections. Nifty, right? Moving on, we then start the "rm_wrap" div.

Step 7

Carrying on from above, paste in the following code:

That is one large piece of code! Explanation - using a php foreach loop, each option type is evaluated on a case-by-case basis. We use a switch-case technique for this. The switch variable is the options - the cases are matched and evaluated. Notice the 'break' statement after each case? This is to prevent something known as the 'fall-through' property. When a case is matched, all successive cases are also executed. This means that if we match case 3, cases 4,5 etc. are also executed. We don't wan't that, right? So use a break to stop the switch-case.

If there is an "open" type option - nothing is done. If there is a "close" type options, two divs are closed. The "title" option is only used once - it is an introduction to the theme options. For each of the types "text" (input type="text"), "select" (dropdowns), "checkbox" and "textarea" (its obvious what those mean), the corresponding input is displayed. Notice the <div class="clearfix"> - it's used for clearing floats, which we will do later.

Step 8

We're coming to the end of that rather massive function. Paste in the code below:

For a "section" type option, I've used a counter variable $i. This keeps track of the sections number and conactenates it to the name of the submit button, to have unique submit buttons. There is also a last form at the end for resetting all options. The image used is going to be a transparent image used in our jQuery-fication. Use this very last piece of code to bring our functions into play:

That tells WordPress to add the admin menu.

Step 9

And voila! We have our own awesome admin panel page with a separate menu position for itself. So let's check it out - click the link. And yuck. That has got to be the most ugly admin panel page ever. So let's call upon our good friend, CSS! Create a new folder in the nettuts/ directory and name it "functions". Create a new CSS file there - functions.css. Paste in the following code:

I won't explain anything here; it's pretty clear what each CSS declaration does, and you're free to customize the layout for your own theme.

Step 10

So now we have a nice CSS file. But how do we add it to the page? After all, we don't have direct access to the <head> of the document. Remember that blank mytheme_add_init() function we wrote in Step 4? That will come in handy. Change it to this:

That adds the functions.css file to the head. The location of the file is determined by the template directory.

Step 11

Go look at the page now. Pretty nice looking, isn't it? But then, you ask, whats the '+' icon for? Well, thats where jQuery comes in!. Create a new file rm_script.js in the nettuts/functions/ folder. Paste in the following code:

What this does is - once the DOM loads, all the rm_options slide up. When the '+' icon is clicked, the inactive class is removed from the image and the active class added - making it a '-' icon. The reverse is done when the '-' icon is clicked. The rm_options is then slid up or down(determined by the current CSS state) using the slideToggle function - pretty simple. To add this script, the same mytheme_add_init() function is used. Change it to:

The jQuery script will now be active. Gp check it out. Personally, I think its beautiful!

Step 12

Now that we have our theme options page all set up, I'll just run you through using the options. The code to use the options is as follows:

That will fetch the nt_color_scheme options. See the examples below:

The variety of uses is limited only by your imagination.


I hope you've learned something in this tutorial. This isn't your standard options panel. This one doesn't use tables, it's jQuery enhanced, uses awesome CSS, and is extremely easy to use. The point of this tutorial is to learn - you could always replace collapsible panels with tabs, for example, or even something more advanced. Use your creativity! Feel free to discuss or ask questions in the comments!

WooThemes has since released version two of their framework. You can review the details here.

Did you find this post useful?
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.