I’m about to create my own options pages for my WordPress theme and I’m a bit confused.
I’ve seen a lot of themes that used arrays to generate options pages, it looked like this:
$options = array (
array( "name" => "Welcome Message",
"type" => "title"),
array( "type" => "open"),
array( "name" => "Title",
"desc" => "Enter a title to display for your welcome message.",
"id" => $shortname."_welcome_title",
"std" => "",
"type" => "text"),
);
Plus a few other functions that generated front-end, tables, forms etc.
On the other hand, there are “static” options pages from Codex ( http://codex.wordpress.org/Creating_Options_Pages ).
They look like:
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<?php do_settings( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">New Option Name</th>
<td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Some Other Option</th>
<td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Options, Etc.</th>
<td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
What are pros & cons of these approaches and which one should I use/is preffered?
I would strongly recommend following the Theme Review guidelines for Theme Settings and Data Security. (If you intend for your Theme to be hosted in the WPORG repository, you will be required to follow these guidelines.
Here’s a brief summary:
References
EDIT
So, to clarify: you don’t have to choose either/or here. The Settings API can handle both approaches simultaneously – and in fact, does so quite elegantly.
The
add_settings_field()
call can be generated dynamically, and can use a single callback, with a switch to output field-type-specific form field markup. Here’s how I implement it.