Support

Account

Home Forums Feature Requests Button to Reset to Default Values Reply To: Button to Reset to Default Values

  • The AJAX function

    
    add_action('wp_ajax_my_options_reset', 'reset_default_options');
    function reset_default_options() {
      // the nonce name here needs to be the same as the
      // the nonce name you created when enqueuing the script
      if (!wp_verify_nonce($_POST['nonce'], 'my_options_reset_nonce')) {
        echo false;
        exit;
      }
      // reset values in a repeater
      // this needs to be the name of the repeater
      // you want to clear
      $repeater = 'repeater_name';
      // list of sub field in repeater
      $subfields = array(
        'campo_teste',
      );
      // list of other fields to be reset
      // this is a list of other fields that
      // are not part of a repeater
      $fields = array(
        'campo_texto',
      );
      $repeater_count = intval(get_option('options_'.$repeater, 0));
      for ($i=0; $i<$repeater_count; $i++) {
        foreach ($subfields as $subfield) {
          delete_option('options_'.$repeater.'_'.$i.'_'.$subfield);
          delete_option('_options_'.$repeater.'_'.$i.'_'.$subfield);
        }
      }
      delete_option('options_'.$repeater);
      delete_option('_options_'.$repeater);
      foreach ($fields as $field) {
        delete_option('options_'.$repeater);
        delete_option('_options_'.$repeater);
      }
      echo true;
      exit;
    }