另请参见here 再想一想,这个问题是面向wp的,所以我把它移到这里
我尝试开发一个wordpress插件,它可以顺利升级,更重要的是,我希望用户选择是否升级。
我就快到了,但我必须手动刷新插件设置页面才能看到升级完成。
为了让我的问题更清楚,让我们看看以下代码:
<?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.
}
}
}
在上面的代码中,我添加了一个名为MyPlugin的管理菜单,它有一个页面(名为
myplugin_admin_page
) 由函数生成
myplugin_admin_page()
. 此页面包括一个名为
myplugin_version
一个叫做
ver
, 此字段的真实渲染由
myplugin_upgrade_render()
. 在
add_settings_section()
我们有回电话
myplugin_upgrade_callback()
, 这将进行真正的升级。
问题是,虽然在单击“升级”按钮后更改了数据库,但设置页面似乎没有自动刷新(我还注意到有一个通知说设置已保存,所以可能会重新加载?)从现场开始ver
不是改变。
SO网友:van abel
上面的代码有一些镜像错误,我发现我可以执行以下操作来解决该问题。我一步一步地查看了提交进度,我发现我们可以将信息放在一个节回调中,并在该节之后添加所有需要更新的内容,然后就可以了。
完整的代码如下:
<?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";
}
}
}
}