1

I'm trying to make a custom plugin for WordPress with an admin option page.

In the option page, the user has to be able to check an option and if the checkbox is checked, the plugin should add a section to an HTML form.

The issue I'm having is that the second checkbox does not store its value inside database, so I'm unable to check it. If I change the declaration code order, only the second checkbox is not working.

Please note that my plugin options are working with every component, and I'm having this problem with the checkboxes only.

Here is the checkbox part of my plugin. Are there some conflicts? What am I doing wrong?

function set_options() {
    register_setting( 'checkbox-1-settings', 'my_checkbox_1_name' );
    register_setting( 'checkbox-2-settings', 'my_checkbox_2_name' );
}

function admin_page(){
    ?>
    <form method="post" action="options.php">
      <?php settings_fields( 'checkbox-1-settings' ); ?>
      <?php settings_fields( 'checkbox-2-settings' ); ?>
      <?php do_settings_sections( 'checkbox-1-settings' ); ?>
      <?php do_settings_sections( 'checkbox-2-settings' ); ?>
      <table class="form-table">
        <tr valign="top">
          <th scope="row">Option 1</th>
          <td><input type='checkbox' name='my_checkbox_1_name' <?php checked( get_option('my_checkbox_1_name'), 1 ); ?> value='1'></td>
        </tr>
        <tr valign="top">
          <th scope="row">Option 2:</th>
          <td><input type='checkbox' name='my_checkbox_2_name' <?php checked( get_option('my_checkbox_2_name'), 1 ); ?> value='1'></td>
        </tr>
      </table>
      <?php submit_button(); ?>
    </form>
    <?php
}

function my_plugin() {

  $html = '<form>
  if (get_option('my_checkbox_1_name')) {
    $html .= '<select name="select1" id="test1">
        <option selected disabled>select1</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>';
  }
  else if (get_option('my_checkbox_2_name')) {
    $html .= '<select name="select2" id="test2">
        <option selected disabled>select2</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>';
  }
  return $html . '<input type="submit" value="Go!"></form>' . $content;
}
0

you are registering two separate settings groups for setting fields & the problem might be here.

 function set_options() {
    register_setting( 'checkbox-1-settings', 'my_checkbox_1_name' );
    register_setting( 'checkbox-2-settings', 'my_checkbox_2_name' );
}

so use only one settings group for both fields to see if it works.

function set_options() {
    register_setting( 'checkbox-settings', 'my_checkbox_1_name' );
    register_setting( 'checkbox-settings', 'my_checkbox_2_name' );
}

Update

Get settings API and store in an array.

$options = (array) get_option( 'plugin_options' );

Now $options is an array containing all fields sub fields. and you can get the value of any key by just passing that key with this array.

for example to get a value of check_2

$check_two = $options['check_2'];
  • Hi Abdul, thank you for your suggestion. I tried but unfortunately it doesn't work and this broke the working checkbox too. – Css14 Mar 5 '17 at 0:49
  • @Css14 you are not doing things correctly. take a look at settings API on WordPress Codex to get better understanding of it. there's an easy way to get things done :) – Anwer AR Mar 5 '17 at 7:08
  • this topic may help you resolve your issue. – Anwer AR Mar 5 '17 at 7:19
  • Hi @Abdul, following your hint, I looked at WordPress Settings API and everything seems to be clearer now. I refactored my plugin, and the Settings are saved succesfully and without conflicts. However I have one last question: would you suggest me how to retrieve a saved-in-array variable with get_option()? I have something like: function settings() { $options = get_option('plugin_options'); if($options['checkbox']) { $checked = ' checked="checked" '; } echo "<input ".$checked." id='check_2' name='plugin_options[check_2]' type='checkbox' />"; } Thank you once more. (: – Css14 Mar 6 '17 at 22:49
  • hello @Css14 take a look at my answer updated portion. i have updated and provided an example of retrieving array values from settings API – Anwer AR Mar 7 '17 at 11:01

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.