0

I working on a theme options page for my template and want to integrate a checkbox. The problem is, the checkbox won't save the value. Where is my mistake? Well, I'm clueless...

<?php

add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );

function theme_options_init(){
    register_setting( 'wordpress_options', 'wordpress_theme_options');
}

function theme_options_add_page() {
    add_theme_page('Options', 'Options', 'edit_theme_options', 'theme-options', 'wordpress_theme_options_page' );
}


function wordpress_theme_options_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
    $_REQUEST['settings-updated'] = false; ?>

[...]

<?php if ( false !== $_REQUEST['settings-updated'] ) : ?> 
<div class="updated fade">
    <p><strong>Settings saved!</strong></p>
</div>
<?php endif; ?>

    <form method="post" action="options.php">
        <?php settings_fields( 'wordpress_options' ); ?>
        <?php $options = get_option( 'wordpress_theme_options' ); ?>

        <div id="admininterface">
            [...]
                <p>
                    <p class="before-form">Show RSS Icon:</p>
                    <?php $options = get_option( 'icon-rss-show' ); ?>
                    <input type="checkbox" name="$options[icon-rss-show]" value="1"<?php checked( 1 == $options['icon-rss-show'] ); ?> />
                </p>
            [...]
        </div><!-- #admininterface -->
    </form>
</div>
<?php } ?>
taito
  • 561
  • 2
  • 6
  • 11

1 Answers1

0

Adding another hidden input field will actually make the form save the zero value in the database like this:

<form>
  <input type='hidden' value='0' name='selfdestruct'>
  <input type='checkbox' value='1' name='selfdestruct'>
</form>

Make sure to place the hidden input above your regular one, with the value '0' and the same name as in the regular input field.

That answer is from here: https://stackoverflow.com/a/1992745/4688612

Please send all credit to that poster.

Aleksandar
  • 1,496
  • 1
  • 18
  • 35