functions - How to reset the plugins without deactivate the plugin

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250717a4618662.html

相关推荐

  • functions - How to reset the plugins without deactivate the plugin

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

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信