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

How to Integrate an Options Page into your WordPress Theme


Creating themes to give away or sell is great, but not everyone who uses your theme will have a solid understanding of HTML/CSS. Providing your theme with an options page makes it easier for non-technical users to make theme changes without getting their hands dirty with code. I'll show you how to make one from scratch!

What We're Going to Achieve

Before we begin, what are we actually striving for? What are we going to make customizable about our theme? Well, this tutorial will focus mostly on the programmatic side, but in order to change something about the layout, we'll need to have a few design ideas up our sleeves, as well. Here's what we have:

  • Change a theme's color scheme
  • Add 2x advertising spots
  • Optional sidebar
  • Optional recent tweets panel
  • Optional search box

There are plenty of editable elements we could add to a theme, but after today's tutorial which illustrates how to implement the five features listed above, you should have a good feel for how to create your own options page.

1. Creating the Required Files

We're going to concentrate entirely on the options page first before we start making the theme change. First, we need a place to put all of our code that's going to manage the options page. All this code is going to resides in a file called functions.php, which will go inside of our themes folder. So, if our theme is called 'example,' then the path to our functions file would be wp-content/themes/example/functions.php.

We don't need to tell WordPress to include our functions.php file, it's automatically called during the WordPress execution cycle.

2. Creating the Options Page

So first we need to actually create the blank options page - the canvas for our work. To do so, we must inform WordPress about the new options page that we wish to create. We accomplish this task by using actions. Actions are called at a specific time in WordPress' execution; so, for example, when the menu in the dashboard is being created, the action admin_menu is called. We can tie functions to these actions; so we can execute our functions at specific times. Here's the basic functions we need to create our options page.

Our first function, themeoptions_admin_menu is simply there to add a link to our page in the admin sidebar, it also tells WordPress what function to call to display the page which, in our case, is themeoptions_page().

The parameters for the add_theme_page() function are as follows:

  • Page Title - Theme Options
  • Menu Title - Theme Options
  • Capability - edit_themes
  • Handle - The current file
  • Function - themeoptions_page()

If you have the theme activated, then, in the Themes drop down menu, you should see your new link to an options page which is currently blank. We now have the canvas for our options page, and we can begin implementing the forms, fields and back end of our options functionality.

3. Adding Options and Fields

Ok, so that page is looking a bit lonely; let's add the form and fields that the user will interact with to make changes to the theme. It's important to note you can style this page however you wish, even adding tabbed dialogs if you want; but for our tutorial, we're going to make use of the default classes used by WordPress. This way it saves us the time of reinventing the wheel, and makes our options page blend in with the rest of the dashboard.

The code for our page should reside in our themeoptions_page() function, so we're going to add our code in like this;

First we create the predefined wrap class for the page. Then, we add a quick header with a default icon. Finally, our form; we need a hidden value so that we can check if it has been submitted. We also need a button that's going to submit the form, and, again, we use a predefined class for this. Here's what we have so far:

Now we have our basic structure. So if we check back to the beginning of this tutorial, we planned to implement five options.

  • First of all, we're going to allow the theme users to change the color scheme. For this, we'll require a drop down list of color schemes available.
  • Secondly, we'll have two advertising spots. We'll need two input fields for the image URLs, and two input fields for the links.
  • Third is the optional sidebar: a simple check box.
  • Fourth is the optional Twitter stream in the sidebar. We'll need a checkbox and an input field for the username.
  • Finally we'll need an optional search box: another check box.

4. Updating The Database

So far, we have an options page with a form that submits data to itself, via POST. The next logical step is to take the data submitted, and insert it into the WordPress database. To do so, we're going to create a new function called themeoptions_update(). This function will be called by themeoptions_page(); so add this code to the very top of themeoptions_page();

Of course, the next step is to create the update function. Remember, for the sake of this tutorial, I'm not adding validation or sanitization to my code. When creating a plugin, you should always check the input from your users. However it's beyond the scope of this tutorial. Now you could rely on WordPress to validate your input, but it's best to do so yourself, to make sure.

Anywhere in the functions.php file, add this block of code:

The update function, as the name suggests, updates an option you can store in the database. If the option doesn't exist, then WordPress creates it. We've prepended all our options with mytheme_, just to make sure we don't overwrite options that another plugin/theme might be using.

So far, we have a settings page that can save our options, however, when we hit submit, and our options are stored, our form is blank when we re-visit it. This is because we need to load our values back from the database.

5. Fetching Options

We now need to fetch our options so that we can populate the settings form. This is an easy one, and we'll do it by using the get_option() function. We could use variables, but for this example, we'll simply echo the output of the function where it should go in the form. Our new code looks like this;

So there you have it. The back end has now been completed. We're able to update our options in the database, so all that's left to do is make the theme react to these changes in the options page. This works similarly to how we updated the options fields: we fetch the option and then either display it or use it as a conditional to do something else. Let's get started with the front end.

6. Changing the Theme

Changing the Colour Scheme

This is going to deal with CSS and such, which isn't what this tutorial is about. So we'll skim over how you'd approach this. One of the best ways to go about this is to have a default stylesheet that uses a colour scheme such as black, then a series of alternative colour schemes. For example style.css could style your layout black, but including red.css AFTER including style.css.

A helpful CSS tip is to use the !important tag. Use this tag in the stylesheets that change colours of the layout to ensure that those properties will, in fact, be used. In order to actually switch the stylesheets, add this code to the header.php file of your layout where the stylesheet would be included:

Adding advertising spots

This section is meant to show you the theory. In real life, you'd probably use a plugin or something like "Buy Sell Ads." So say we've got two spots for our adverts to go in. Similar to above, we simply output the option where it needs to go, like so:

Optional Sidebar

This time, we'll use the option as a conditional to decide whether or not we should output a block of code, that in this case would display a sidebar. Of course, your layout should be able to still display properly if a sidebar is not visible; so that one is up to you. The following code will be added wherever you'd usually call your sidebar, usually mine is called from header.php.

Optional Recent Tweets

For this little option, we'll need to do two things. If tweets are enabled we'll first need to display the area where the tweets should appear, we'll also need to insert a block of JavaScript before the </body> tag to get the tweets from a specific user.

We need to place the following where we actually want the tweets to appear:

Next we need to place this code before the </body> tag, which most probably resides in footer.php;

Tip: why not try and add your own option to change how many tweets are fetched above?

Optional Search Box

Finally, we come to our optional search box, which functions just as our sidebar did. We use the option as a conditional and display code dependent on the user's choice.

Wrapping Up

I hope you've learned the basics of how to create an options page for your theme, and make it function properly. I've tried to skip a few of the tedious bits, like the actual CSS and the validation that should be in place, primarily because it was outside of the scope of this tutorial.

As usual, if you have any questions at all, leave a comment below! Thanks for reading!

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