January is Theme Month

In January 2009 I will be concentrating on themes and themeing.

Got a theme question, or conundrum? Let me know.

Adding settings to admin pages

I want to introduce you to a new function in 2.7 that might help to reduce the number of admin pages in your WordPress installation. The function allows plugin authors to extend the existing admin pages instead of creating new ones.

Here’s a quick example:

add_action('admin_init', 'add_my_settings_field');
 
function add_my_settings_field(){
 
// The fields are:
//the id the form field will use
//name to display on the page 
//callback function
//the name of the page
//the section of the page to add the field to
add_settings_field('my_field_id' , 'My Field Caption' ,
			'my_field_callback' , 'writing' , 'default');
 
//register the setting to make sure it gets checked
register_setting('writing','my_field_id');
}
 
function my_field_callback(){
 
//echo out the text field, drop down, or other type of field.			
echo '<input name="my_field_id" id="my_field_id" type="text"
 value="'.attribute_escape(get_option('my_field_id')).'" class="regular-text code" />';
 
}

In WordPress 2.7 admin pages are made of pages, sections and fields. In the above example I have added a new field to the writing page (Settings > Writing) in the default section.

This not only creates the table rows and table header for the form field, it also saves the value for you. The above code is everything you need to add the new question.

The value can be accessed again using get_option(’my_field_id’).

You could just as easily add it to the remote_publishing section, by changing the last attribute in the add_settings_field function in the example to ‘remote_publishing’.

If you take a look at the options-writing.php page in wp-admin you will see that beneath each section there is a function, do_settings_fields, that lists the page and the section in its arguments; i.e. do_settings_fields(’writing’, ‘remote_publishing’)

These tell you the settings you need to add fields in that part of the page.

I think this is a very important step in helping to reduce the clutter in WordPress, no longer will a new admin page be needed for a single setting if it could sit in an existing page.

Easier Admin Pages

I came across a page on the codex today that showed me a way of creating admin pages that I didn’t know about before. This allows simple plugin settings page to be created that update themselves without any additional PHP.

By creating an admin page with the appropriate nonce, action, and page_options fields you create an options page that gives WordPress enough information to handle the form submission, save the options, and handle the redirect back to the same page again.

Before now I had been creating pages to do a lot of this manually.

You can read the full details about creating options pages on the codex.

Describing 2.7

How can I describe the latest 2.7 design? Weird is the word that springs to mind, but, they are innovating so with an open mind let’s take a look.

First up I am presented the new dashboard; an odd series of icons and a list of dashboard widgets:

The icons down the site work in two ways: hover over them and get a drop-right menu or expand the menu and the open in-line

Moving over most of the dashboard widgets gives you an edit link so you can choose to change the settings.

Not all of these widgets will let you edit them yet but some do. And you can move them around by clicking and holding on the name.

At the top of every page there is a page options settings that opens an window to let you choose what to include on that page.

The changes mean you can actually have a very minimalist post page that is quite conducive to writing without clutter:

I am sure there is some way to go with this yet but it looks interesting. What do you think?