Save Checkbox value in Plugin [duplicate]

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 ); ?> />