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)