WordPress Theme Options Framework

Update: New Version

I’ve released a newly optimized version of the Theme Options Framework that now only makes two calls to the database rather than one call per option.

Recently I’ve been doing alot of work on my themes. I started off with the idea to make my theme work user friendly by adding a options page. So I started looking for how to do this, and came across this page and some useful code. So I took this and started modifying and trying different things and now I think I’ve got a very useful framework and I wanted to share with others. I’ll continue below as this will be fairly long post.

Update

Mike from Split Visonz has updated my framework to include a checkbox and multiple selection elements. Multiple selection is a dropdown list where you can select multiple items. You can download the new version below and I’ll explain the new additions as well.

First you’ll need to download the framework file:

After you have the controlpanel.php file uploaded to your server into your theme’s main directory you’ll need to add this line to your functions.php file

<?php require_once(TEMPLATEPATH . '/controlpanel.php'); ?>

Now you’ll need to start editing the array values to add your options.

$options = array (
    array(  "name" => "Radio Selection Set",
            "desc" => "This is a descriptions",
            "id" => $shortname."_radio",
            "type" => "radio",
            "std" => "3",
            "options" => array("3", "2", "1")),

    array(  "name" => "Text Box",
            "desc" => "This is a descriptions",
            "id" => $shortname."_text_box",
            "std" => "Some Default Text",
            "type" => "text"),

    array(  "name" => "Bigger Text Box",
            "desc" => "This is a descriptions",
            "id" => $shortname."_bigger_box",
            "std" => "Default Text",
            "type" => "textarea"),
  
    array(  "name" => "Dropdown Selection Menu",
            "desc" => "This is a descriptions",
            "id" => $shortname."_dropdown_menu",
            "type" => "select",
            "std" => "Default",
            "options" => array("Default", "Option 1", "Option 2")),
  
    array(  "name" => "Checkbox selection set",
            "desc" => "This is a descriptions",
            "id" => $shortname."_checkbox_menu",
            "type" => "checkbox",
            "std" => "Default",
            "options" => array("Default", "Option 1", "Option 2")),
  
    array(  "name" => "Multiple selection box",
            "desc" => "This is a descriptions",
            "id" => $shortname."_multi_select_dropdown_menu",
            "type" => "multiselect",
            "std" => "Default",
            "options" => array("Defaults", "Option 1s", "Option 2s"))
);

I’ve included in the framework examples of all the types of elements you can add, you can have as many options as you like. Notice there are four different types text, textarea, radio, select, multiple select, and checkbox. Also pay attention to the type of the file to the first two variables that you need to edit to reflect your theme.

$themename = "ThemeFullName";
$shortname = "themeShortName";

After you have your option page the way you like you’ll need some way of get the variables onto other pages of you theme. You’ll need to add these next few lines to every page on which you plan to use the theme variables.

<?php 
global $options;
foreach ($options as $value) {
	if($value['type']!="checkbox" and $value['type']!="multiselect"){
		if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { 
		$$value['id'] = get_settings( $value['id'] ); }
	}
	else{
		if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = explode(",",$value['std']); } else { 
		$$value['id'] = explode(",",get_settings( $value['id'] )); }
	}
} ?>

Now you can use the variables on this page. The variables will look like this let’s assume you set $shortname = “theme”, then your variables will look like this $theme_radio, $theme_text_box, $theme_bigger_box, $theme_dropdown_menu. Also keep in mind that you can add one than one of any kind of element, you can also rename any of the elements for easier variable management. Where “id” => $shortname.”_radio”, is defined you can change the _radio part to reflect what the option is actually for. For example I have mine set to chose how many columns my theme displays, so my variable is “id” => $shortname.”_columns”.

Update

To check the value of your multiple selection or checkbox variables you’ll need to use this. Where Item is the value that your checking that is selected or checked.

if(in_array("Item",$theme_checkbox))

About Jeremy Clark

Small town IT worker with interests in all things technological and technical. Biggest interests are in web development especially the WordPress publishing platform and the community supporting it. Currently developing and maintain the free WordPress theme Techozoic. I'm also always available for hire.
This entry was posted in guides, tech, wordpress and tagged , , , . Bookmark the permalink.

36 Responses to "WordPress Theme Options Framework"

Leave a reply

Comments will be sent to the moderation queue.