0

I am currently having a problem with my custom made plugin. It actually logs all the actions/filters fired to a file (or to screen). I need to update my options in the db somehow when submitting the form. Here's the code listing itself:

class WP_Test_Logging_Plugin {

//required for the file name
private $yymmddhhmmss;
private $data;

public function __construct() {
    add_action( 'all', array( $this, 'log_to' ) );
    add_action( 'admin_menu', array( $this, 'menu' ) );
    add_action( 'admin_print_styles', array( $this, 'plugin_theme_style' ) );
    add_action( 'template_redirect', array( $this, 'on_admin_form_submit' ) );
    $this->yymmddhhmmss = date( 'YMDHis' );
    if ( get_option( 'where_to_log_to' ) == false ) {
        update_option( 'where_to_log_to', 1 );
    }
    $this->data = get_option( 'where_to_log_to' );
}

the method I use to process the form

public function on_admin_form_submit() {
    if ( isset( $_POST['selection'] ) ) {
        update_option( 'where_to_log_to', $_POST['selection'] );
    }
}

Is there a way to go about it? Thanks!

  • @s_ha_dum, could you help me with this? – Alex Herrmann Sep 29 '13 at 22:35
  • @toscho , excuse me. I am just new to stackexchange. Trying to learn using it in an appropriate way. – Alex Herrmann Sep 29 '13 at 23:14
0

First, template_redirect is a front end hook. It never fires on your backend save. You need to choose a backend hook such as admin_init to hook your function.

Secondly, based on your pastebin from you other question, your code has the checked attribute hard-coded for both of your radio boxes, thus causing it to default to the last one all the time. You are going to want to edit your form like so:

<input type="radio" <?php checked($this->data, 1); ?> value="1" name="selection" >

Third, you need to make sure that both of your radio buttons have a value attribute.

Fourth, you need to update the option when you save so...

public function on_admin_form_submit() {
    if ( isset( $_POST['selection'] ) ) {
      if ($_POST['selection'] == 1){
        update_option( 'where_to_log_to', 1 );
      } else {
        update_option( 'where_to_log_to', 2 );
      }
    }
    $this->data = get_option( 'where_to_log_to', 1 );
}

Notice that I explicitly saved the values 1 and 2.

I think that's got it.

| improve this answer | |
  • ok. i will test it in a moment. could you tell me if it is allowed to share contact details in the comments? i just could send you my zip package. – Alex Herrmann Sep 29 '13 at 22:58
  • tested. it is still not changing my option. – Alex Herrmann Sep 29 '13 at 23:02
  • In a comment to my answer on your other question you stated "But I seen to run into the same trouble with endless loop." Is that not correct? Do you have an infinite loop or not? – s_ha_dum Sep 29 '13 at 23:36
  • Actually, nope. I'm testing the plugin and there's no infinite loop. I still wonder why it doesn't change my option in the database. It definitely changes the option if i state in in the __constructor(). But nowhere else. – Alex Herrmann Sep 30 '13 at 0:14
  • @AlexHerrmann : see the edit – s_ha_dum Sep 30 '13 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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