function set_copyright_options() {
delete_option(\'ptechsolcopy_notice\');
delete_option(\'ptechsolcopy_reserved\');
add_option(\'ptechsolcopy_notice\',\'Copyright ©\');
add_option(\'ptechsolcopy_reserved\',\'All Rights Reserved\');
}
register_activation_hook(__FILE__, \'set_copyright_options\');
您好,我使用该代码使其成为默认插件,同时停用和激活插件。但我需要的选项,使其使用重置按钮在管理方面,使其默认不停用插件?如何在不停用插件的情况下重置插件
2 个回复
最合适的回答,由SO网友:Mike Madern 整理而成
您可以使用will(重新)设置默认选项值来创建另一个函数:
function wpse_91307_set_option_defaults() {
$options = array(
\'ptechsolcopy_notice\' => \'Copyright ©\',
\'ptechsolcopy_reserved\' => \'All Rights Reserved\'
);
foreach ( $options as $option => $default_value ) {
if ( ! get_option( $option ) ) {
add_option( $option, $default_value );
} else {
update_option( $option, $default_value );
}
}
}
然后你可以改变你的set_copyright_options()
此功能:function set_copyright_options() {
delete_option( \'ptechsolcopy_notice\' );
delete_option( \'ptechsolcopy_reserved\' );
wpse_91307_set_option_defaults( );
}
当你击中reset
button,你唯一要做的就是执行wpse_91307_set_option_defaults()
作用SO网友:RRikesh
使用add_menu_page
创建页面。在回调函数中,添加带有重置按钮的窗体:
function reset_my_options() {
add_menu_page( \'Reset Options\', \'Reset Options\', \'manage_options\', \'reset-options\', \'reset_option_page\' );
}
function reset_option_page() {
if ( isset( $_POST[\'reset_options\'] ) && $_POST[\'reset_options\'] === \'true\' ) {
delete_option(\'ptechsolcopy_notice\');
delete_option(\'ptechsolcopy_reserved\');
}
?>
<div class="wrap">
<h2>Reset options</h2>
<form action="<?php echo admin_url( \'admin.php?page=reset-options\' ); ?>" method="post">
<input type="submit" value="Click to reset plugin options" style="float:left;" />
<input type="hidden" name="reset_options" value="true" />
</form>
</div>
<?php
}
您还可以添加nonces 为了进一步的安全。顺便说一句,你可以使用update_option
在插件激活中,而不是delete_option
和add_option
.
结束