get_option() returning empty

时间:2014-05-26 作者:Nyecodem

我正在为主题和get_option() 用于检索设置的仅返回string(0) "" 即使有复选框字段。我想要的是检索设置,以便使用它们来确定是否应使用选中/取消选中复选框checked(). 以下是相关代码:

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 个回复
SO网友:panos

我看不到$option_name 已声明,但不知道它包含什么。您正在使用该变量($option_name) 在复选框的名称属性中。

您需要为该复选框字段的name属性指定选项的名称。这是register\\u setting函数中的第二个参数。所以如果$option_name 如果未声明,则应为其指定值“theme\\u settings”。

您还需要设置复选框的value属性。

之后,您可以在复选框中使用类似的内容:

<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\' : \'\'; ?> />
我认为有一些代码缺失,并且可能在settings_fieldsdo_settings_sections.

结束

相关推荐