In my plugin settings page, I have a list of checkboxes that store the value into an array. While the it works as expected, whenever I uncheck all of the checkboxes and save those changes I get the following error:

Notice: Undefined index: remove_field in ...\settings-profile.php on line 19

Below is the code in question:

if ( isset( $_POST['dsbl_options'] ) && !empty( $_POST['dsbl_options'] ) ) {
    update_option( 'dsbl_remove', $_POST['remove_field'] ); // (Line 19)

The rest of the source code can be found here: GitHub

From doing research online for a similar error, I see that the recommendation is to wrap $_POST['remove_field'] with an isset. However, this does not store the values form the checkboxes in the plugin settings.

As mentioned above, this error shows up only when all of the checkboxes are unchecked and saved. It looks like there is an issue when saving an empty value in the array. Although the plugin settings works as expected, I'd like to fix this.

  • Add a test for $_POST['remove_field'] to avoid trying to use it if it's empty. – jdm2112 Aug 15 '16 at 15:16
  • May you please elaborate on what you mean by adding a test to $_POST['remove_fiel‌​d']? – Ethan O'Sullivan Aug 15 '16 at 15:26
  • You are testing for a value (twice) in $_POST['dsbl_option'] but you are not testing if $_POST['remove_field'] has a value before you use it. If you can post more of your code, I could post a full answer for you. As it is, I do not know what dsbl_option or 'remove_field' refer to. – jdm2112 Aug 15 '16 at 15:32
  • I've updated the question with a link to the source file – Ethan O'Sullivan Aug 15 '16 at 15:36

I'm not seeing the original snippet from before so I'm not sure of the context anymore. The undefined index notice you are getting is telling you that $_POST['remove_fiel‌​d'] does not exist. If no checkbox in your array was set, the $_POST array will not have an index for 'remove_field', and you'll see the error.

To prevent this from happening, ensure it exists:

if ( isset( $_POST['remove_field'] ) ) {
    // do stuff with the values

When in an OO context the more frequent solution is to bail out if no value exists so you would reverse your condition:

if ( !isset( $_POST['remove_field'] ) ) {
  • I've re-added the snippet to the question. Is there a way to set a blank value when no checkboxes are selected? I've added the isset, however it won't let me uncheck all of the checkboxes. – Ethan O'Sullivan Aug 15 '16 at 19:17
  • That is completely up to you. You can push any value you wish to represent no checkboxes selected. An empty/null string, 0, etc would be obvious choices. – jdm2112 Aug 15 '16 at 21:08
  • I was able to solve the question between your post and someone else, thank you for helping me solve it +1 – Ethan O'Sullivan Aug 17 '16 at 0:07

As mentioned here (thanks to bcworkz), I was able to fix the issue by inserting NULL if all of the checkboxes are unchecked:

if ( isset( $_POST['dsbl_options'] ) && !empty( $_POST['dsbl_options'] ) ) {
    if ( array_key_exists('remove_field', $_POST )) {
        update_option( 'dsbl_remove', $_POST['remove_field'] );
    else { // When all options are unchecked, set array to null
        update_option( 'dsbl_remove', NULL );

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.