See also here after a second think, this question is wp oriented, so I move it to here.
I try to develop a wordpress plugin, which can be upgraded smoothly and more importantly, I want the user to choose whether to upgrade or not.
I almost got there, but I have to refresh the plugin setting page manually to see the upgrade complished.
To make my question more clear, let us see the following code:
<?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">
<form action='options.php', method='post'>
<?php
setting_fields('myplugin_admin_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');
add_settings_section('myplugin_version', //section id
__('Version infomation', 'myplugin') , 'myplugin_upgrade_callback', 'myplugin_upgrade_page'
//page
);
add_settings_field('ver', 'Current Version:', array(
$this,
'myplugin_upgrade_render'
) , 'myplugin_upgrade_page', //page
'myplugin_version', //section
array(
'field' => 'ver'
));
add_settings_field('upgrade_confirm', 'Upgrade?', array(
$this,
'myplugin_upgrade_confirm_render'
) , 'myplugin_upgrade_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['ver'];
//we add the hidden filed to save 'ver' option
echo "<input type='text' size='4' disabled='disabled' value='<?php
echo $value; ?>'>";
echo "<input name='myplugin_options[<?php
echo $field; ?>]' type='hidden' value='<?php
echo $value; ?>'>";
}
public function myplugin_upgrade_confirm_render($args)
{
$this->options = get_option('myplugin_options');
$field = $args['field'];
$value = $this->options['upgrade_confirm'];
?>
<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
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);
//I want to add a function/action/filter here to refresh the setting page but failed.
}
}
}
In the above code, I add a admin menu called MyPlugin, which has a page(called myplugin_admin_page
) generated by the function myplugin_admin_page()
. This page includes a section called myplugin_version
with a filed called ver
, the real render of this filed is done by myplugin_upgrade_render()
. In the add_settings_section()
we have a callback myplugin_upgrade_callback()
, which will do the real upgrade.
The problem is that, although the database is changed after clicking on Upgrade button, but it seems that the setting page is not automatically refreshed (I also note there is a notification says that the settings saved, so maybe it reload?) since the field ver
is not change.