This kind of setup skeleton works for me to process the custom options page input :
<?php
/*
Plugin Name: Settings Skeleton
*/
if(!class_exists('MySettings')){
function call_my_settings() {
return new MySettings();
}
if (is_admin()){
add_action( 'init', 'call_my_settings' );
}
class MySettings{
public function __construct(){
add_action('admin_menu', array(&$this, 'add_menu'));
add_action('admin_init', array(&$this,'settings') );
}
public function add_menu(){
add_options_page(__('My Plugin Settings','myplugindomain'), 'My Plugin Settings', 'manage_options', __FILE__, array(&$this,'options_page'));
}
public function settings() {
register_setting( 'my_settings', 'my_settings', array(&$this,'validate') );
}
public function validate($input) {
// do validation
$input['mytext']=esc_attr($input['mytext']);
return $input;
}
public function options_page(){
?>
<div class="wrap">
<div class="icon32" id="icon-options-general"><br></div>
<h2><?php _e('My plugin settings','myplugindomain'); ?></h2>
<form method="post" action="options.php">
<?php settings_fields('my_settings'); ?>
<?php $settings = get_option('my_settings'); ?>
<table class="form-table">
<tr>
<th scope="row" colspan="2">
<h3><?php _e('General settings','myplugindomain'); ?></h3>
</th>
</tr>
<tr>
<th scope="row"><?php _e('My text','myplugindomain'); ?> </th>
<td>
<input type="text" size="70" name="my_settings[mytext]" value="<?php echo $settings[mytext]; ?>" />
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
}
This will give you the following options page:
How do you hook your settings()
function ?
In the code example above the settings()
functions is hooked into the admin_init
action, i.e.
add_action('admin_init', array(&$this,'settings') );