WordPress Settings API – a quick implementation

Updated: v1.3 released making use of Namespaces.

I’m currently building a plugin that requires some configuration options, and although there are easy ways of saving plugin ‘options’ in WordPress, there is a push towards making use of the Settings API.  As I like to do things correctly, I thought I’d investigate it.

WordPress does make a habit of creating mountains out of molehills sometimes, and the Settings API seems to be a fantastic example of this.  Trying to follow the instructions on the initial page got me hopelessly lost, and it was only when I went through Otto’s WordPress Settings API tutorial that I begin to understand how to implement it.

As I could see myself using it for various plugins in the future, I wanted to create something that can be re-used quickly and easily.  So I’ve devised a little routine that means you can quickly implement settings for your plugin without even having to edit any code (beyond the values/structure of an array).

The ob_settings zip file contains a php file which has the function settings and how to configure it for your settings requirements.  It’s pretty expandable, allowing you to add multiple fields and sections if you need.

To implement it, all you need to to is add the files to your plugin folder, and link to the php file in your plugin file using include() and then edit the top of the php file in your favourite text editor.  I’d recommend doing a global search/replace within the PHP file to replace ‘ob_settings’ to something unique to your plugin, as if more than one plugin makes use of this routine then it’ll cause problems.

You still need to have a basic understanding of how to call options so you can use them in your plugin – see Settings API for info on that.

I’m not suggesting this is a particularly great way to implement Settings API, and it could obviously be expanded significantly, but it appears to work so I’m happy to share it and save others the trouble I went through creating a Settings page.

v 1.2 - Amended this function to make the values class based. Thanks toKarl Cohrs to correcting the errors.

v 1.3 - Made use of Namespaces functionality, to help if multiple versions of this routine if it is used in multiple plugins.  You must has PHP 5.3.x installed for this to work.

Download: ob_settings.zip (both v1.2 and v1.3 are in the zip file, you only need to use one version)

15 Responses to “WordPress Settings API – a quick implementation”

  1. Karl said:

    Jul 16, 11 at 12:20 pm

    Wow, I’ve been going to make a class like this for the last 6 months… just haven’t gotten around to it yet.

    About to try this out, looks like it will be a massive timesaver.

    Think you need to add a donate button :)

  2. Karl said:

    Jul 16, 11 at 4:36 pm

    Alright so a few hiccups….

    1. missing a few commas in the $sections array for starters…

    2. invalid callbacks in plugin_admin_init().

    ob_settings::plugin_section_text etc should be…

    array( ‘ob_settings’, ‘plugin_section_text’ )

    or ideally

    array( &$this, ‘plugin_section_text’ )

  3. Wordpress Settings API Class - Karl Cohrs said:

    Jul 16, 11 at 5:45 pm

    [...] Benson created a class for quick implementation of the WordPress Settings API. Basically, this can achieve rapid [...]

  4. Olly said:

    Jul 16, 11 at 10:54 pm

    Hi Karl,

    Thanks for this and thanks for fixing those errors. I’ve uploaded the amended version with your credit.

    Olly

  5. Olly said:

    Jul 16, 11 at 11:05 pm

    Hi Karl (and anyone else),

    If you want to make a donation, please see my About page.

  6. Mark said:

    Aug 12, 11 at 12:51 am

    Olly,
    This is great. I’ve modified your class so I can add_submenu_page instead of option page. This way I can use it to create unlimited number of submenu pages under an existing option page.

    This class saves tons of time. I’ll definitely be donating.

    Thanks,
    Mark.

  7. Flavio said:

    Nov 08, 11 at 8:37 pm

    Hi,

    How can I make a password field appears as a password (asterisc), not as a text input field?

    Thanks

  8. Olly said:

    Nov 08, 11 at 9:56 pm

    Hi Flavio,

    You’d need to change the type in the input code in plugin_setting_string to password. Obviously if you change it directly, you’ll then only get password fields, so you’d have to add a variable to control whether the field should be password or text, or create a second function specifically for passwords. Hope this makes sense.

    Olly

  9. Christopher McCulloh said:

    Nov 23, 11 at 4:19 am

    Parse error: syntax error, unexpected T_STRING in /home/cmcculloh/blog.cmcculloh.com/wp-content/themes/continuum/functions/ob_settings_v1_3.php on line 14

    Is all I get when I try and run your file :/

  10. Olly said:

    Nov 23, 11 at 8:07 am

    Sounds like you don’t have PHP 5.3.x installed. Namespaces were only introduced in version 5.3. Use v1.2 of the plugin if you can’t install PHP 5.3.x.

  11. Tobias Florek said:

    Jan 29, 12 at 1:00 pm

    you might consider adding a license to the file(s). as is, any use not from you will be a copyright infringment… :/

  12. Olly said:

    Jan 29, 12 at 8:52 pm

    Hi – Fair point, although the about us page has my CC licence details: http://code.olib.co.uk/about/

  13. Tobias Florek said:

    Mar 02, 12 at 6:13 pm

    aww. that’s bad. “cc by” is incompatible with gpl (wordpress is gpl-licensed).

    you might want to reconsider. and just use a 2-claused bsd license which might match your intention as well.

  14. best scripts said:

    May 18, 12 at 12:02 am

    best scripts…

    [...]Code» Blog Archive » WordPress Settings API – a quick implementation[...]…

  15. Wordpress settings API is PITA | mark kaplun's blog said:

    Dec 15, 12 at 11:49 am

    [...] hints that the answer is probably “not really”. To quote Olly Benson from yet another settings API tutorial/example (emphasize mine) WordPress does make a habit of creating mountains out of molehills sometimes, and [...]


Leave a Reply