Add a new checkbox in theme options

To answer the first part of your question: your settings form already has the checkbox case covered; so if you add a new checkbox input, you don’t need to add anything to the switch. This code will apply to all checkbox inputs you add:

case 'checkbox':
?>
    <input type="checkbox" name="vertigo_theme_options[<?php echo esc_attr( $option['id'] ); ?>]" id="<?php echo esc_attr( $option['id'] ); ?>" value="true" <?php echo ( 'true' == $value ) ? 'checked="checked"' : ''; ?> />
<?php break;

More on switch from the PHP.net manual and from w3schools.

I’m a bit confused by the wording of your question:

I’ve got the option to show up in the panel via the array

vs.

I’ve noted below… where it appears I need to add it in the “create form” section in the php that builds the form.

So: does the new checkbox currently appear in the Settings page form, or does it not appear?

EDIT

Since the setting field appears properly in the settings form, I’m going to try to tackle the next two issues: the option not saving properly, and the option not outputting properly.

First things first: is the option saving its value properly in the DB, based on the settings form field selection?

If it’s not saving correctly, then the issue may be that you’re not checking for it properly. With a checkbox, if the checkbox isn’t enabled, then the entire parameter is omitted from the POST data. In that case, in order to save its value in the DB, you need to check for the option value being set, e.g.:

<?php
$vertigo_options['disable_comments_link'] = ( isset( $_POST['disable_comments_link'] ) ? true : false );
?>

Note: you’ll want to adjust for however you are sanitizing the form data. If you were using the Settings API, it would look more like this:

<?php
$output['disable_comments_link'] = ( isset( $input['disable_comments_link'] ) ? true : false );
?>

Does this get us closer?

EDIT 2

So we know that the option is saving properly in the DB. Now we just need to get it to output properly in the template.

Next question: are the options stored in the DB discretely, or as an array?

From your settings form markup, it appears that the options are stored as an array, so we need to call get_option() on the array, and then use values in that array:

<?php
$vertigo_theme_options = get_option( 'vertigo_options' );
?>

Note: to find the actual database entry name, refer to the vertigo_get_theme_options() function. You could also just call this function:

<?php
$vertigo_theme_options = vertigo_get_theme_options();
?>

Either way, now you should be able to reference your option in $vertigo_theme_options; e.g.:

<?php if ( 'true' == $vertigo_theme_options['disable_comments_link'] ) { ?> 
<?php comments_template( '', true ); ?>
<?php } ?>

Does that get us there?

EDIT 3

option_name under wp_options is vertigo_theme_options

Then try this:

<?php
$vertigo_theme_options = get_option( 'vertigo_theme_options' );
?>

<?php if ( 'true' == $vertigo_theme_options['disable_comments_link'] ) { ?> 
<?php comments_template( '', true ); ?>
<?php } ?>

I’m just about 100% sure this should do it.

Leave a Comment