Found good sample of wordpress settings page code. Copy-pasted it with minor changes. Almost everything seems to be working and saving no problem. Except color and another thing (separate question). Does wordpress require some extra code for color picker, or am I doing something wrong?
Here is the code
add_action('admin_init', 'ozh_sampleoptions_init' );
add_action('admin_menu', 'ozh_sampleoptions_add_page');
function ozh_sampleoptions_init(){
register_setting( 'ozh_sampleoptions_options', 'ozh_sample', 'ozh_sampleoptions_validate' );
}
function ozh_sampleoptions_add_page() {
add_options_page('Ozh\'s Sample Options', 'Sample Options', 'manage_options', 'ozh_sampleoptions', 'ozh_sampleoptions_do_page');
}
function ozh_sampleoptions_do_page() {
?>
<div class="wrap">
<h2>Ozh's Sample Options</h2>
<form method="post" action="options.php">
<?php settings_fields('ozh_sampleoptions_options'); ?>
<?php $options = get_option('ozh_sample'); ?>
<table class="form-table">
<tr valign="top"><th scope="row">A Checkbox</th>
<td><input name="ozh_sample[option1]" type="checkbox" value="1" <?php checked('1', $options['option1']); ?> /></td>
</tr>
<tr valign="top"><th scope="row">A color</th>
<td><input name="ozh_sample[optioncolor1]" type="color" value="#000" /></td>
</tr>
<tr valign="top"><th scope="row">Some text</th>
<td><input type="text" name="ozh_sample[sometext]" value="<?php echo $options['sometext']; ?>" /></td>
</tr>
<tr valign="top"><th scope="row">Another text</th>
<td><input type="text" name="ozh_sample[blabla]" value="<?php echo $options['blabla']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
function ozh_sampleoptions_validate($input) {
$input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
$input['sometext'] = wp_filter_nohtml_kses($input['sometext']);
$input['blabla'] = wp_filter_nohtml_kses($input['blabla']);
$input['optioncolor1'] = wp_filter_nohtml_kses($input['optioncolor1']);
return $input;
}
To execute a task after a option has been updated, you can use updated_option
action hook:
add_action( 'update_option_{option-name}', 'update_new_option_name_callback', 10, 2 );
function update_new_option_name_callback( $old_value, $value ) {
//Do something
}
Try this one with your option name
add_action( 'update_option_ozh_sample', 'update_new_option_name_callback', 10, 2 );
function update_new_option_name_callback( $old_value, $value )
{
//Do something
}