最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to reset the plugins without deactivate the plugin

programmeradmin0浏览0评论
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');

Hi I use the code to make it plugin default while deactivate and activate plugin .But i need the options to make it using the reset button in the admin side to make it default without deactivate the plugin ?

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');

Hi I use the code to make it plugin default while deactivate and activate plugin .But i need the options to make it using the reset button in the admin side to make it default without deactivate the plugin ?

Share Improve this question asked Mar 18, 2013 at 10:16 masterzoranmasterzoran 532 silver badges7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You could make another function with will (re)set the default option values:

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 );
    }
  }
}

Then you could change your set_copyright_options() function into this:

function set_copyright_options() {
  delete_option( 'ptechsolcopy_notice' );
  delete_option( 'ptechsolcopy_reserved' );

  wpse_91307_set_option_defaults( );
}

When you hit the reset button, the only thing you have to do is execute the wpse_91307_set_option_defaults() function.

Use add_menu_page to create the page. In the callback function, add a form with a reset button:

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
}

You can also add nonces to it for further security.

BTW, you could have use update_option in your plugin activation instead of delete_option and add_option.

发布评论

评论列表(0)

  1. 暂无评论