Checkbox not saving in custom settings page wordpress

The value I have for my checkbox does not save when the page is reloaded. It saves the value but does not update the checkbox when it is reloaded.

Currently I am using the settings in the wordpress codec. The label values are saved and repopulate when the page is reloaded but the checkbox does not. This is super simple but for some reason I cannot get it working.

Read More

So to clarify, the checkbox value is saved and I can use it, however the checkbox does not relfect the latest selection i.e. checked or unchecked. It is always unchecked.

<?php
// create custom plugin settings menu
add_action('admin_menu', 'baw_create_menu');

function baw_create_menu() {

    //create new top-level menu
    add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/opd.png', __FILE__));

    //call register settings function
    add_action( 'admin_init', 'register_mysettings' );
}


function register_mysettings() {
    //register our settings
    register_setting( 'baw-settings-group', 'new_option_name' );
    register_setting( 'baw-settings-group', 'some_other_option' );
    register_setting( 'baw-settings-group', 'option_etc' );
    register_setting( 'baw-settings-group', 'option_name' );
}

function baw_settings_page() {
?>
<div class="wrap">
<h2>Your Plugin Name</h2>

<form method="post" action="options.php">
    <?php settings_fields( 'baw-settings-group' ); ?>
    <?php do_settings_sections( 'baw-settings-group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
        </tr>

       <input name="option_name" type="checkbox" value="1" <?php checked( '1', get_option( 'option_name' ) ); ?> />

        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
        </tr>
    </table>

    <?php submit_button(); ?>

</form>
</div>
<?php } ?>

Related posts

Leave a Reply

4 comments

  1. What if you do store the option value in a variable and use variable instead?

    $option_name = get_option( 'option_name' );
    
    <input name="option_name" type="checkbox" value="1" <?php checked( 1,  $option_name); ?> />
    
  2. Assuming that the value of get_option( 'option_name' ) is 1.

    Try checked( 1, (int)get_option( 'option_name' ) );

    If it doesn’t work, test if checked( 1, (int)get_option( 'option_name' ) ) echos ‘checked’ or just returns it.

    If that didn’t work I’d try using if ( get_option(option_name) == 1 ) echo 'checked="checked"'; instead of the checked function.

    If that doesn’t work, then it’s most likely some caching issue.