Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I got some option pages for my theme. I did this whit add_menu_page, add_submenu_page. I further got settings with register_setting and there i got add_settings_section and add_settings_field(s). The field callback functions come from a class that auto-generates my fields. I then built a form callback function (for add_(sub)menu_page like the following.

public function render() {
  $this->notify();
  ?>
   <div class="wrap">   
    <div id="icon-themes" class="icon32"><br /></div>
    <h2><?php echo $this->page_title; ?></h2>
    <p><?php _e('The developer mode allows access to some of your most important rapid prototyping tools.', MY_TEXTDOMAIN); ?></p>

    <div class="postbox-container">
     <div class="postbox ">
      <div class="inside">

       <form action="options.php" method="post">

        <?php
        settings_fields( MY_OPTIONS );
        do_settings_sections(__FILE__);
        ?>

        <p class="submit">
         <input class="button-primary" name="Submit" type="submit" value="<?php _e('Save Changes', MY_TEXTDOMAIN); ?>" />
        </p>

       </form>

       <form method="post">
        <p class="submit">
         <input class="button-secondary" name="Reset" type="submit" value="<?php _e('reset', MY_TEXTDOMAIN); ?>" />
         <input name="action" type="hidden" value="reset" />
        </p>
       </form>

      </div>
     </div>
    </div>

   </div>
   <div class="clear"></div>
  <?php 

 }

 private function notify() {
  if ( isset($_POST['save']) ) :
   ?>
   <div id="message" class="updated fade">
    <p><?php sprintf(__('$1$s Options update %2$ssuccessfull%3$s.', MY_TEXTDOMAIN), $this->name, '<strong>', '</strong>'); ?></p>
   </div>
   <?php 
  elseif ( isset($_POST['reset']) )  :
   delete_option( MY_OPTIONS );
   ?>
   <div id="message" class="updated fade">
    <p><?php sprintf(__('$1$s Options have been %2$sdeleted%3$s.', MY_TEXTDOMAIN), $this->name, '<strong>', '</strong>'); ?></p>
   </div>
   <?php 
  endif;
 }

(This is all done in a class)

I can set default values, change and save them and everything works out fine. But one problem stays: I can't delete my options and can't reset them to the defaults. Does anybody know how to achieve that? Thanks!

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You should (and must for security) use validation function, that is part of Settings API. I don't seem to see it in your code. There you should be able to simply pass empty value for option under circumstances it should be saved as disabled.

Of course you can also just use regular Options API functions to access and modify your settings directly, from any appropriate place in code.

share|improve this answer
    
The Q is quite old and the (class/) code evolved slightly. Since then i found the submit_button(); function, but nothing more. To clarify: What i want to do is to offer a "reset to defaults" button below the form, next to the "save" button. –  kaiser Jun 1 '11 at 0:12
    
@kaiser obviously, I am just cleaning up "unanswered" pile bit by bit :) I don't remember - can you catch which button was pressed if there are several buttons in form? Then just create additional button and when it is used to submit form - nuke everything to defaults in validation function. –  Rarst Jun 1 '11 at 7:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.