Easy WordPress Theme Options

For the most up-to-date information visit the plugin page

I’ve been getting really annoyed every time I want to add options to my theme and I have to copy a huge mess of code that just isn’t fun to use. You know what I mean. Most theme developers now include configurable options in their themes, which if you ask me, isn’t super easy.

So the other day I set out to write a class that theme developers could package with their file and have easy access to theme options. It’s as easy as adding my theme_options.php file to an include folder in your theme directory, and then adding this code to your functions.php (Using your option values obviously).

Download my ThemeOptions class

require_once( 'includes/theme_options.php' );

define( 'THEME_NAME' , 'Theme Template' );
define( 'THEME_SHORTNAME' , 'themetpl' );

ThemeOptions::add( 'sample_text_field', 'Its a text field'  , array( 'desc' => 'A description of the theme option', 'type' => 'text', 'default' => 'Default Value' ) );
ThemeOptions::add( 'sample_textarea'  , 'Its a text area'   , array( 'desc' => 'A description of the theme option', 'type' => 'textarea', 'default' => "Text\nTest" ) );
ThemeOptions::add( 'sample_checkbox'  , 'Its a checkbox'    , array( 'desc' => 'A description of the theme option', 'type' => 'checkbox', 'default' => 'enabled' ) );
ThemeOptions::add( 'sample_select'    , 'Its a select thing', array( 'desc' => 'A description of the theme option', 'type' => 'select', 'default' => 'Option 2', 'values' => array( 'Option 1', 'Option 2', 'Option 3' ) ) );
ThemeOptions::add( 'sample_radio1'    , 'Its a radio field' , array( 'desc' => 'A description of the theme option', 'type' => 'radio', 'default' => 'val1', 'values' => array( 'val1' => 'Text 1' , 'val2' => 'Text 2' ) ) );
ThemeOptions::add( 'sample_radio2'    , 'Its a radio field' , array( 'desc' => 'A description of the theme option', 'type' => 'radio', 'default' => 'val2', 'values' => array( 'val1' => 'Text 1' , 'val2' => 'Text 2' ) ) );

Now this may seem confusing at first, but if we expand it out, you’ll see it’s similar to other WordPress functions (I did the code like that for that specific reason):

$args = array(
    'desc' => 'This is a description of the option',
    'type' => 'text',
    'default' => 'This is the default value'
);

ThemeOptions::add( 'option_id', 'Option Name', $args );

And to get a value:

$val = ThemeOptions::get('simple_text_field');

Isn’t that easier that what you’ve been doing? I’ve encapsulated my code into a nice class to make everything super simple. The results should look similar to this (Located in Appearance > Theme Options):

Screenshot of the Theme Options Page

Download my ThemeOptions class

  5 comments

  1. Brad   •  

    Cool, I’ll be coming back. I’ve already got this integrated into one of the theme’s I’m using, and I’m planning on incorporating it in a theme I’m doing from the ground up basically.

    -Brad

  2. Brandon Wamboldt   •     Author

    Glad you like it, make sure to check back in the upcoming weeks, as I plan on adding categories and a few other features too it. I’m basically looking at existing themes to see what type of options they have so I can implement an easier to use and more standardized version into my class.

    -Brandon

  3. Brad   •  

    I’ve been meaning to update a theme to have some options VS manually editing it, and this gives me a real good excuse to update the theme.

    Thanks for this class!

    -Brad

  4. Kyle Walters   •  

    Very handy thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>