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 */

/* 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 */

function vigtig_besked_html_page() {
<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><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' ); ?>

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

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


<?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>'; ?>


<?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');}

1 Answer 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' />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.