0

I'm having trouble saving multiple checkboxes using WordPress settings api. I can add multiple values, but only the last checked box gets saved.

This is what I currently have

function uwcc_checkbox_field_1_render(  ) { 

$options = get_option( 'uwcc_settings' );
?>
<input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1]' <?php checked( $options['uwcc_checkbox_field_1'], Mastercard ); ?> value='Mastercard'>
    <label>Mastercard</label>
<input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1]' <?php checked( $options['uwcc_checkbox_field_1'], Visa ); ?> value='Visa'>
   <label>Visa</label>
<input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1]' <?php checked( $options['uwcc_checkbox_field_1'], Amex ); ?> value='Amex'>
   <label>Amex</label>
<?php

}

I did try placing [] at the end of all of the uwcc_settings[uwcc_checkbox_field_1] like below but then no settings save.

    <input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1][]' <?php checked( $options['uwcc_checkbox_field_1'], Mastercard ); ?> value='Mastercard'>
<label>Mastercard</label>

How do I get it to save all fields?

1

The field name should be uwcc_settings[uwcc_checkbox_field_1][], i.e. turn the field into an array.

Secondly, when saved as an array, you should use in_array() along with the checked() function. But first, let's put the uwcc_checkbox_field_1 option's values into a variable:

$uwcc_checkbox_field_1 = isset( $options['uwcc_checkbox_field_1'] ) ?
  (array) $options['uwcc_checkbox_field_1'] : [];

Now you call checked() like so — the 1, for example in the first line, means the default value (as in <input value="Mastercard" type="checkbox".../>) is Mastercard:

checked( in_array( 'Mastercard', $uwcc_checkbox_field_1 ), 1 )
checked( in_array( 'Visa', $uwcc_checkbox_field_1 ), 1 )
checked( in_array( 'Amex', $uwcc_checkbox_field_1 ), 1 )

The full code, tried & tested working: (re-indented for clarity)

function uwcc_checkbox_field_1_render() {

    $options = get_option( 'uwcc_settings', [] );

    $uwcc_checkbox_field_1 = isset( $options['uwcc_checkbox_field_1'] )
        ? (array) $options['uwcc_checkbox_field_1'] : [];
    ?>
    <input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1][]' <?php checked( in_array( 'Mastercard', $uwcc_checkbox_field_1 ), 1 ); ?> value='Mastercard'>
        <label>Mastercard</label>
    <input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1][]' <?php checked( in_array( 'Visa', $uwcc_checkbox_field_1 ), 1 ); ?> value='Visa'>
       <label>Visa</label>
    <input type='checkbox' name='uwcc_settings[uwcc_checkbox_field_1][]' <?php checked( in_array( 'Amex', $uwcc_checkbox_field_1 ), 1 ); ?> value='Amex'>
       <label>Amex</label>
    <?php

}

And don't forget to quote the checkbox values; e.g. 'Mastercard' and not Mastercard. See below, assuming that the Mastercard is not a PHP constant:

checked( $options['uwcc_checkbox_field_1'], Mastercard )   // bad
checked( $options['uwcc_checkbox_field_1'], 'Mastercard' ) // good

checked( in_array( Mastercard, $uwcc_checkbox_field_1 ), 1 )   // bad
checked( in_array( 'Mastercard', $uwcc_checkbox_field_1 ), 1 ) // good
  • 1
    Perfect. It works. Thank you. I added [] to the end of the checkbox_field_1 to allow multiple selects. I'm still learning but having fun at the same time. – John Cook Feb 14 at 3:56
  • And I still make mistakes sometimes.. sorry about the missing []! – Sally CJ Feb 14 at 3:59
  • It's all good Sally CJ. I'm learning all about hide and seek. ';"} I spent 30 minutes last night trying to figure out what I did wrong only to find that I spelt "don't" inside single quotes instead of double, lol – John Cook Feb 14 at 6:09
  • Yep, that sounds familiar to me, like when I was writing some JS code, I concatenated some strings using . and not +... :p But just keep on learning, because we all do. :) – Sally CJ Feb 14 at 6:24

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.