ThemeOptions for Theme Developers

Download Link: Download my ThemeOptions class

Features:

  • Easily add a page for editing theme options in the admin
  • Easily get theme options
  • Options are automatically prefixed in the add and get process

Planned Features:

  • Adding groups/categories
  • Adding a call back function option
  • JQuery saving/groups
  • Input validation (Might just leave that to call back functions)
  • Add some filters

Overview:

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).

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):

Screen shots:

Screenshot of the Theme Options Page
Screenshot of the Theme Options Page

Download my ThemeOptions class