I'm building a settings page for a theme and the get_option()
that I'm using to retrieve the settings returns only string(0) ""
even though there is a checkbox field. What I want is to retrieve the settings so I can use them to determine whether the checkbox should be checked/unchecked with checked()
. Here's the relevant code:
function theme_settings_init(){
register_setting('theme_settings', 'theme_settings', 'sanitize_settings');
add_settings_section('general_settings', 'General Settings', 'general_settings', 'theme_settings.php');
function general_settings(){}
$home_blocks_args = array(
'type' => 'checkbox',
'id' => 'home_blocks_checkbox',
'name' => 'home_blocks_checkbox',
'desc' => 'Toggle the home blocks section on the front page',
'std' => '',
'label_for' => 'home_blocks_checkbox',
'class' => 'settings_field'
);
add_settings_field('display_home_blocks', 'Display Home Blocks', 'display_settings_field', 'theme_settings.php', 'general_settings', $home_blocks_args);
function display_settings_field($args){
extract($args);
$option_name = 'theme_settings';
$options = get_option($option_name);
switch($type){
case 'checkbox':
$id = stripslashes($id);
$id = esc_attr($id);
?><input type="checkbox" id="<?php echo $id; ?>" name="<?php echo $option_name; ?>[<?php echo $id; ?>]" value="true" <?php echo $option_name[$id] == "true" ? 'checked' : ''; ?> /><?php
echo ($desc != '') ? "<span class='description'>$desc</span>" : "";
break;
}
}
}
add_action('admin_init', 'theme_settings_init');
function sanitize_settings($input){
return $input;
}
function theme_settings_add_page() {
add_theme_page( __('Theme Settings'), __('Theme Settings'), 'edit_theme_options', 'theme_settings.php', 'theme_settings_do_page');
}
add_action('admin_menu', 'theme_settings_add_page');
function theme_settings_do_page(){ ?>
<div class="section panel">
<h1>Custom Theme Options</h1>
<form method="post" enctype="multipart/form-data" action="options.php">
<?php
settings_fields('theme_settings');
do_settings_sections('theme_settings.php');
?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>