This is a sample code but very similar to my code.
class JW_Options{
public $options;
public function __construct(){
//delete_option('jw_plugin_options');
$this->options = get_option('jw_plugin_options');
$this->register_setting_and_fields();
}
public static function add_menu_page(){
add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, array('JW_Options', 'display_options_page'));
}
public static function display_options_page(){
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My Theme Options</h2>
<?php print_r(get_option('jw_plugin_options'));?>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php settings_fields('jw_plugin_options'); ?>
<?php do_settings_sections(__FILE__); ?>
<P class="submit">
<input name="submit" type="submit" class="button-primary" value="Save Changes" />
</P>
</form>
</div>
<?php
}
public function register_setting_and_fields(){
register_setting('jw_plugin_options', 'jw_plugin_options', array($this, 'jw_validate_settings'));// 3rd param = optional call back function
//register_setting('jw_plugin_options', 'jw_plugin_options');
add_settings_section('jw_main_section', 'Main Settings', array($this, 'jw_main_section_cb'), __FILE__); // id,title of section, cb, which page?
add_settings_field('jw_banner_heading', 'Banner Heading', array($this, 'jw_banner_heading_setting'), __FILE__, 'jw_main_section');
add_settings_field('jw_logo', 'Your Logo', array($this, 'jw_logo_setting'), __FILE__, 'jw_main_section');
add_settings_field('jw_color_scheme', 'Your Desired Color Scheme', array($this, 'jw_color_scheme_setting'), __FILE__, 'jw_main_section');
}
public function jw_main_section_cb(){
//optional
}
public function jw_validate_settings($plugin_options){
print_r($plugin_options);
}
/*
*
*Inputs
*
*/
// Banner Heading
public function jw_banner_heading_setting(){
echo "<input name='jw_plugin_options[jw_banner_heading]' type='text' value='{$this->options['jw_banner_heading']}' />";
//echo "<input name='jw_plugin_options[jw_banner_heading]' type='text' value='' />";
}
public function jw_logo_setting(){
echo '<input type= "file" />';
}
public function jw_color_scheme_setting(){
$items = array('Red', 'Green', 'Blue', 'Yellow', 'Black', 'White');
echo "<select name='jw_plugin_options[jw_color_scheme]'>";
foreach ($items as $item){
$selected = ($this->options['jw_color_scheme']== $item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
echo "</select>";
}
}
add_action('admin_menu', function(){
JW_Options::add_menu_page();
});
add_action('admin_init', function(){
new JW_Options();
});