Looking at WordPress Theme Options

Categories: Blog, Development, WordPress
Tags: , , , , ,

Mar 23
2011

As a WordPress theme developer I enjoy having the ability to add options to my themes – and I believe my clients enjoy this too.  What I didn’t always enjoy was building the options page over and over again for each theme.  I knew there had to be a better way to do this so I went hunting around online and found two key articles that got me thinking.

The first article was by David Gwyer over at Press Coders. I liked the way he approached putting the various input fields in their own functions. This means that an option page with multiple text input fields could simply call that one function over and over again.

The second article, by Dan Harper, is a bit older but made use of a feature that I thought was excellent.  Dan uses an array (or arrays) to handle the setup of the options page.  He then uses a switch statement to cycle through the array and spit out the code for the different input fields.

I liked both approaches and thought that it would be better to get the best of both worlds.

  1. Make use of the WordPress Settings API.
  2. Put input fields in separate functions for repeat calls.
  3. Use an array to handle the various options and settings.

However, there were a few things I thought were missing and wanted to add.

  1. Let the script generate better code.
    • Add a unique id and name for each input field.
    • Add label for the input field.
  2. Allow for optional descriptions on input fields.
  3. Modify the output for the WordPress Settings API add_settings_section() to allow for a description paragraph.
  4. Set default values that will be inserted into the WordPress Options (database) table when the theme is activated.
  5. Allow the option to remove all values from the WordPress Options (database) table when the theme is no longer active (switched to a new theme).
  6. Have a simple method to retrieve options – all of them in an array or a specific option’s value.
  7. The ability to use a shortcode so the option values can be used inside WordPress (pages/posts/etc.).
  8. Add multiple select listbox and a date field – date field uses jQuery UI Datepicker.

The Result

The result of all this work is DMG WordPress Theme Options – which I will be releasing to the public as soon as I draft up the ‘manual’ on how to use it.  That shouldn’t take too long, I’ve made it pretty easy to use.

 

Comments

Leave a comment

 

Leave a comment

Read comments

 



Please do not submit your comment more than once. It will appear once it has been approved.