• Hi, I am trying to make a simple modification to a theme that I downloaded and I am having some trouble. I added a checkbox to the theme options form but I can’t get the checkbox to reflect the value saved to the options after submitting. It always remains checked.

    This statement reflects the way I have tried to accomplish this:

    <p><input type="checkbox" name="usethemegravatar" value="true" checked=<?php echo get_option('usethemegravatar'); ?> />


    Also at the end I have a hidden input in the form:

    <input type="hidden" name="page_options" value="feedid,greeting,welcomemessage,usethemegravatar" />

    I have also tried setting up an array with an initial value in it and then changing the value when the form is submitted but I haven’t been able to get that working either. I am new to WordPress and PHP so I am still learning how it all works. Thanks for any help!

    This is the full editoptions() from function.php

    function editoptions() {
    
      <div class='wrap'>
      <h2>Theme Options</h2>
      <form method="post" action="options.php">
      <?php wp_nonce_field('update-options') ?>
      <p><strong>Greeting Heading:</strong></p>
      <p><input type="text" name="greeting" value="<?php echo get_option('greeting'); ?>" /></p>
      <p><strong>Welcome Message:</strong>If you don't specify a message, a default will be used.</p>
      <p><textarea name="welcomemessage" cols="100%" rows="10"><?php echo get_option('welcomemessage'); ?></textarea></p>
      <p><strong>Post your "Subscription Form Code" from the Feedburner website here.</strong></p>
      <p><textarea name="feedid" cols="100%" rows="10"><?php echo get_option('feedid'); ?></textarea></p>
    <p></p>
    
      <p><input type="checkbox" name="usethemegravatar" value="true" checked=<?php echo get_option('usethemegravatar'); ?> />
    Use the theme default Gravatar. If not the WordPress default will be used.</p>
    
      <p><input type="submit" name="Submit" value="Update Options" /></p>
      <input type="hidden" name="action" value="update" /> 
    
      <input type="hidden" name="page_options" value="feedid,greeting,welcomemessage,usethemegravatar" />
      </form>
      </div>
      <?php
      }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Most tutorials show a checkbox code for theme option pages that doesn’t exactly work as expected, I modified like this and it seems to work:

    <?php if( get_option($value['id']) ){ $checked = "checked=\"checked\""; } elseif ( empty($value['id']) ) { if ($value['std']){ $checked = "checked=\"checked\""; } else { $checked = ""; } } else { $checked = ""; } ?>
    		<div class="box-title"><?php echo $value['name']; ?><input class="check-field" type="<?php echo $value['type']; ?>"  name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> /></div>

    Sorry the correct code was:

    <?php if( get_option($value['id'] ) ){ $checked = "checked=\"checked\""; } else { if ( $value['std'] === "true" ){ $checked = "checked=\"checked\""; } else { $checked = ""; } } ?>
    		<div class="box-title"><?php echo $value['name']; ?><input class="check-field" type="<?php echo $value['type']; ?>"  name="<?php echo $value['id']; ?>" id="<?php echo $value['id']

    You can also use: <?php checked(true, get_option( $value[‘id’] )); ?>

    see it here: http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-admin/includes/template.php.source.html#l374

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Checkboxes in theme options’ is closed to new replies.