The above code has some mirror errors, and I find I can do the following to solve the problem. I have a step by step look of the submit progress, and I find that we can put the infomation in a section callback and add all the need to be updated stuffs after this section, then it will work.
The complete code like this:
<?php
class myplugin_admin_setting_class
{
private $options;
public function __construct()
{
add_action('admin_menu', array(
$this,
'myplugin_admin_menu'
));
add_action('admin_init', array(
$this,
'myplugin_admin_init'
));
}
public function myplugin_admin_menu()
{
add_options_page('My Plugin Setting Page', 'MyPlugin', 'manage_options', 'myplugin', array(
$this,
'myplugin_admin_page'
));
}
public function myplugin_admin_page()
{
$this->options = get_option('myplugin_options');
?>
<div class="wrap">
<h2> Settings </h2>
<form action='options.php', method='post'>
<?php
@settings_fields('myplugin_upgrade_page');
@do_settings_sections('myplugin_admin_page');
submit_button('Upgrade');
?>
</form>
</div>
<?php
}
public function myplugin_admin_init()
{
register_setting('myplugin_upgrade_page', 'myplugin_options');
$this->options = get_option('myplugin_options');
$this->options['ver']='old version';
update_option('myplugin_options', $this->options);
add_settings_section('myplugin_version', //section id
__('Version infomation', 'myplugin') , array( $this, 'myplugin_upgrade_callback'), 'myplugin_admin_page'
//page
);
add_settings_field('ver', 'Current Version:', array(
$this,
'myplugin_upgrade_render'
) , 'myplugin_admin_page', //page
'myplugin_version', //section
array(
'field' => 'ver'
));
add_settings_field('upgrade_confirm', 'Upgrade?', array(
$this,
'myplugin_upgrade_confirm_render'
) , 'myplugin_admin_page', //page
'myplugin_version', //section
array(
'field' => 'upgrade_confirm'
));
}
public function myplugin_upgrade_render($args)
{
$this->options = get_option('myplugin_options');
$field = $args['field'];
$value = $this->options[$field];
//we add the hidden filed to save 'ver' option
echo "<input type="text" size="4" disabled='disabled' value="". $value ."">";
echo "<input name="myplugin_options[". $field ."]" type="text" value="".$value."">";
}
public function myplugin_upgrade_confirm_render($args)
{
$this->options = get_option('myplugin_options');
$field = $args['field'];
$value = $this->options[$field];
?>
<input type="checkbox" name="myplugin_options[<?php
echo $field; ?>]" id="<?php
echo $field; ?>" <?php
checked($value, true); ?> value="1" />
<?php
}
public function myplugin_upgrade_callback()
{
//check user option of upgrade true/false
$this->options = get_option('myplugin_options');
if ($this->options['upgrade_confirm']) {
echo "Do upgrade!"; //the fake code
$options = get_option('myplugin_options');
$options['upgrade_confirm'] = false; // No upgrade again
$options['ver'] = 'new version'; //update the 'ver' filed
update_option('myplugin_options', $options);
}else{
echo "Please check the upgrade field<br>";
if($this->options['ver']!='new version')
{
echo "display upgrade details";
}
}
}
}