Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');}
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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' />
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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