Can’t save checkbox option

I know this is a mess, but can someone tell me why I can’t save to checkbox option in this plugin?

/* Runs when plugin is activated */
register_activation_hook(__FILE__,'vigtigt_besked_install'); 

/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__, 'vigtigt_besked_remove' );

function vigtig_besked_install() {
/* Creates new database field */
add_option("vigtigt_data", 'Default', '', 'yes');
add_option("tweakfunction1", 'Default', '', 'yes');
}

function vigtig_besked_remove() {
/* Deletes the database field */
delete_option('vigtigt_data');
delete_option('tweakfunction1');
}

/*———————————————————————*/
function vigtig_besked_html_page() {
?>
<div>
<h2>Vigtige beskeder!</h2>

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>

<table width="900">
<tr valign="top">
<th align="left" width="140" scope="row">Skriv din tekst her:</th>
<td width="700">
<input size="76" name="vigtigt_data" type="text" id="vigtigt_data" value="<?php echo get_option('vigtigt_data'); ?>" />
(f.eks. Undervisning er aflyst pga. sne!)</td>

</tr>
<tr><th align="left" width="140" scope="row">Funktion1:</th><td>

<input size="76" name="tweakfunction1" type="checkbox" id="tweakfunction1" checked="<?php get_option( 'tweakfunction1' ); ?>" />
<?php echo  get_option( 'tweakfunction1' ); ?>
</td></tr>
</table>

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="vigtigt_data,tweakfunction1" />

<p>
<input type="submit" value="<?php _e('Gem ændringer') ?>" /> 
</p>

</form>





<?php if (!current_user_can('manage_options'))  {
        wp_die( __('You do not have sufficient permissions to access this page.') );
    }
    echo '<div class="wrap">';
    echo '<p>Here is where the form would go if I actually had options.</p>';
    echo '</div>'; ?>








</div>


<?php }

if ( is_admin() ){

/* Call the html code */
add_action('admin_menu', 'vigtig_besked_admin_menu');

function vigtig_besked_admin_menu() {
add_menu_page('Vigtigt', 'Vigtige beskeder!', '1','vigtig_besked', 'vigtig_besked_html_page');}
}

Related posts

Leave a Reply

1 comment

  1. Not sure where you’re adding this functionality, but I don’t see anywhere in this code where you use the update_option() function. Above your form HTML you should check if the form has posted data, then update the option.

    //Do nonce checking here and 
    $tweak = $_POST['tweakfunction1'] ? $_POST['tweakfunction1'] : '';
    update_option('tweakfunction1', esc_html($tweak));
    

    WordPress includes a function called checked() that checks against a certain value. Your input tag has no value attribute as well. I added it below with the checked function.

    <input size="76" name="tweakfunction1" type="checkbox" id="tweakfunction1" <?php checked('foobar', get_option('tweakfunction1'));?> value='foobar' />