Chomper Stomping jQuery/JavaScript/CSS 3/HTML 5, Java/PHP/Python/ActionScript, Git, Chrome/Firefox Extensions, Wordpress/Game/iPhone App Development and other random techie tidbits I've collected

23Nov/110

WordPress Settings API – Adding Options to Existing Page

blue-xl

Adding new options to an existing page in the dashboard in wordpress can be maddening. I've literally spent 15+ hours dealing with this horrible API at this point. To the point where I wrote two different wrappers for it.

Here are some notes chiseled along the way for any poor soul following me down this obtuse path.

Today I'll focus on adding options to an existing page. Hopefully soon I'll do one on making a new page.

Checklist/Overview:
1. Hook into the admin init action (add_action('admin_init'))
2. Create your section (add_settings_section)
3. Output hidden fields so settings will save (settings_fields)
4. Create your fields (add_settings_field and register_setting)

Your options will be available through the usual "get_option" means...

Step 1) Hook into the admin init action:

Step 2) Create your init function (that you just hooked into the admin_init action):

Step 3) Create your own section register API

Step 4) Create your section callback functions

It is really annoying that the API doesn't allow you to pass params to this callback function, which means you have to define each one individually...

Step 5) Create your own field register API

Step 6) Create your global callback function

Step 7) Register your section(s)

This goes in that init function you made in step 2

Step 8) Register your field(s)

This goes in that init function you made in step 2

Here's an example of a completed version of all this. This one adds a crap ton of custom thumbnail size options to the media panel. It is a really bad example as it is overly complex:

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.