WP Options detecting which button was pressed

I am creating a custom plugin and have an options page. My variables are saving when I click the save button, but I want to add a second button and detect which button was pressed. I’ve been trying to put names in the buttons and was hoping to detect them via isset $_POST[‘name’] {}, but when I click the save or the other button, it just saved my variables, but does not put anything in the POST variable. As you can see in the code, one button saves the variables from a form the other button that saves and runs some script using those variables. The problem is that I need the page to know what button was clicked after the reload, you can see my attempt on discerning which button was clicked on the very bottom.
I would prefer a php solution, so I can progressive enhance. Thanks!

<div class="wrap">
<h2>Config Me Bro</h2>
<form method="post" action="options.php">
    <?php settings_fields('aug_options'); ?>
    <?php $options = get_option('data_value'); ?>
    <label for="">Checkbox</label>
          <input name="data_value[option1]" type="checkbox" value="1" id="" <?php checked('1', $options['option1']); ?> />
     <label for="general_title">Title</label>
           <input type="text" name="data_value[sometext]" id="general_title" value="<?php echo $options['sometext']; ?>" />

    <p class="submit">
        <?php submit_button('Save Changes', 'primary', 'save_config', false); ?>
        <?php submit_button('Run Config', 'secondary', 'run_config', false); ?>
    </p>
</form>
</div>
<pre> <?php print_r($_POST);?></pre>
<?php
}

/* Run Config Settings */
if (isset($_POST['run_config'])){
     echo '<h1>I am running</h1>';
}
/* Save config Settings */
elseif (isset($_POST['save_config'])){
   echo '<h1>Saved it</h1>';
}

Related posts

Leave a Reply

1 comment

  1. Just in case you are still looking for the answer. Also, for future reference so that I can find it if ever lost… Here is what I’ve done and it seems to work ok.

    When you use register_setting(‘group’, ‘setting’) make sure to use the 3rd parameter and define a callback function. In the callback you will be able to access the submitted options, but also the $_POST variables. $_POST[‘submit’] is the one you are looking for.

    In practice….

    register_settings('my_plugin_settings_group', 'my_plugin_settings', 'my_plugin_settings_callback');
    
    function my_plugin_settings_callback( $posted_options ) {
        // $_POST['submit'] contains the value of your submit button
        if( $_POST['submit'] == 'Run Config' ) {
            // your code here
        }
        // $posted_options is an array with all the values submitted so you have to return it.
        return $posted_options;
    }
    

    I hope this helps someone else. I’ve been looking for an answer for ages and then started experimenting.

    • RK