1

I cannot seem to save the value of a checkbox in a plugin I am creating. When I uncheck it and click Save Settings, it reverts to being checked again. I'm not sure where I have gone wrong. Any help would be most appreciated.

//Global Variable
$options = array();

//Options Page
    function iwmp_options_page() {

        if( !current_user_can('manage_options') ) {
            wp_die('You do not have sufficient permission to access this page.');
        }

        global $options;

        if( isset($_POST['iwmp_settings_form_submitted']) ) {

            $hidden_field = esc_html( $_POST['iwmp_settings_form_submitted'] );

            if($hidden_field == "Y") {

                $iwmp_single_images = esc_html( $POST['iwmp_single_images'] );

                $options['iwmp_single_images'] = $iwmp_single_images;

                update_option('iwmp_settings', $options);

            }

        }

        $options = get_option('iwmp_settings');

        if( $options != '' ) {

            $iwmp_single_images = $options['iwmp_single_images'];

        }

        require('plugin-settings-page-wrapper.php');

    }

Inside the 'plugin-settings-page-wrapper.php' file: (Stripped down of course)

<label for="iwmp_single_images">Enable on Single Images:
  <input checked="checked" name="iwmp_single_images" type="checkbox" id="iwmp_single_images" value="1" <? checked( $options['iwmp_single_images'], 1, false );?> />
</label>
1

If a checkbox is unchecked, it doesn't get submitted with the form values. So when using checkboxes, you should have an else statement for isset( $_POST['iwmp_settings_form_submitted'] ) and it basically will be triggered when the checkbox is unchecked. Here's the fixed code.

//Global Variable
$options = array();

//Options Page
function iwmp_options_page() {

    if( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'You do not have sufficient permission to access this page.' );
    }

    global $options;

    // Make sure this is a POST request
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
        if ( ! isset( $_POST['_iwmp_nonce'] ) || ! wp_verify_nonce( $_POST['_iwmp_nonce'], 'iwmp_settings_nonce' ) ) {
            wp_die( 'Cheating, Huh?' );
        }

        if( isset($_POST['iwmp_settings_form_submitted']) ) {

            $hidden_field = esc_html( $_POST['iwmp_settings_form_submitted'] );

            if($hidden_field == "Y") {

                if ( isset( $_POST['iwmp_single_images'] ) ) {
                    $options['iwmp_single_images'] = esc_html( $_POST['iwmp_single_images'] );
                } else {
                    $options['iwmp_single_images'] = '';
                }

                update_option('iwmp_settings', $options);

            }

        }

    }

    $options = get_option('iwmp_settings');

    if( $options != '' ) {

        $iwmp_single_images = $options['iwmp_single_images'];

    }

    require('plugin-settings-page-wrapper.php');

}

I also added a nonce validation in your code(because they're a good practice). In addition to the extra code below, you should also add the following code inside of your <form> in your settings page HTML:

<?php wp_nonce_field( 'iwmp_settings_nonce', '_iwmp_nonce' ); ?>

Using nonces is a good practice that makes your plugin more secure. You can read more on Nonces here: http://codex.wordpress.org/WordPress_Nonces

And the code for your checkbox should be as follows:

<input name="iwmp_single_images" type="checkbox" id="iwmp_single_images" value="1" <?php checked( $options['iwmp_single_images'], 1 ); ?> />

1

Adding to what @Nikola Ivanov Nikolov said you shouldn't be formatting your input like this:

 <input checked="checked">

Basically, what you've done, you've made the checkbox be always checked (notice the checked="checked" value).

Instead, you should be using:

<input type="checkbox" name="iwmp_single_images" value="1" <?php checked( $options['iwmp_single_images'], 1 ); ?> />
  • Removed checked="checked" thank you. Still not saving though I'm afraid. – Huw Rowlands Feb 18 '15 at 10:36

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.