1

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>

1

I can't see where the $option_name is declared, and don't know what it contains. You are using that variable ($option_name) in the name attribute of the checkbox.

You need to give to the name attribute of that checkbox field the name of your option. That is the second argument in the register_setting function. So if $option_name is not declared, you should give it the value "theme_settings".

You also need to set the value attribute of the checkbox.

After that you can use something like this in your checkbox:

<input type="checkbox" id="<?php echo $id; ?>" name="<?php echo $option_name; ?>[<?php echo $id; ?>]" value="some-value" <?php echo $option_name[$id] == "some-value" ? 'checked' : ''; ?> />

I believe that there is some code missing and it is possible that there might be some inconsistencies in settings_fields or do_settings_sections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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