Can't save options

时间:2011-03-15 作者:Mamaduka

我使用设置API创建了主题选项页面,一切正常,但我无法保存选项。我试图找出我错过了什么,但我失败了。知道是什么导致了这个问题吗?

这是我的选项页面的代码

<?php
/**
 * This functions registers default options values for Evolutionary theme 
 * settings
 */
function ev_theme_settings_defaults() {
    $options = array(
        \'color_scheme\' => \'blue\',
        \'copyright\' => \'\',
        \'nav_hide_home\' => false,
        \'nav_hide_sec\' => false,
        \'nav_show_lvl\' => false,
        \'featured_show\' => false,
        \'featured_pause\' => false,
        \'featured_speed\' => 5,
        \'ad1_link\' => \'\',
        \'ad1_image\' => \'\',
        \'ad2_link\' => \'\',
        \'ad2_image\' => \'\',
        \'ad3_link\' => \'\',
        \'ad3_image\' => \'\',
        \'ad4_link\' => \'\',
        \'ad4_image\' => \'\'
    );

    return $options;
    // Change to apply_filters( \'ev_theme_settings_defaults\', $options );
}

/**
 * Create helper function for theme colors
 */
function ev_get_valid_color_schemes(){
    $color_schemes = array(
        \'blue\' => __( \'Blue\', \'evolutionary\' ),
        \'green\' => __( \'Green\', \'evolutionary\' ),
        \'red\' => __( \'Red\', \'evolutionary\' ),
        \'black\' => __( \'Black\', \'evolutionary\')
    );

    return $color_schemes;    
} 

/**
 * Setup initial theme options
 */
function ev_options_init() {
    // Set options equal to defaults
    global $ev_options;
    $ev_options = ev_theme_settings_defaults();
    if ( false === $ev_options ) {
        $ev_options = ev_theme_settings_defaults();
    }
    update_option( \'theme_evolutionary_options\', $ev_options );
}

add_action( \'after_setup_theme\', \'ev_options_init\', 9 );

/**
 * Add theme options page to admin menu
 */
function ev_menu_options() {
    add_theme_page( __( \'Theme Options\', \'evolutionary\' ), __( \'Theme Options\', \'evolutionary\' ), \'edit_theme_options\', \'evolutionary-settings\', \'ev_admin_options_page\' );
}

add_action( \'admin_menu\', \'ev_menu_options\' );

/**
 * Register theme options
 */
function ev_register_options() {
    register_setting( \'theme_evolutionary_options\', \'theme_evolutionary_options\', \'ev_options_validate\' );

/**
 * Defining settings sections and fields
 * fist setting creates sections for general options
 */
add_settings_section( \'ev_settings_general_options\', __( \'General Options\', \'evolutionary\' ), \'ev_settings_general_options\', \'evolutionary\' );

// Creates section for navigation options
add_settings_section( \'ev_settings_nav_options\', __( \'Navigation\', \'evolutionary\' ), \'ev_settings_nav_options\', \'evolutionary\' );

// Section for featured post options
add_settings_section( \'ev_settings_featured_options\', __( \'Featured Posts\', \'evolutionary\' ), \'ev_settings_featured_options\', \'evolutionary\' );

// Section for Advertising options
add_settings_section( \'ev_settings_ad_options\', __( \'Advertising\', \'evolutionary\' ), \'ev_settings_ad_options\', \'evolutionary\' );

/**
 * Created empty callbacks for setions
 */
function ev_settings_general_options() {
    // Put callback code here
}
function ev_settings_nav_options() {
    // callback
}
function ev_settings_featured_options() {
    // callback
}
function ev_settings_ad_options() {
    // callback
}

/**
 * Registered fields
 */
add_settings_field( \'ev_settings_copyright_field\', __( \'Copyright Name\', \'evolutionary\' ), \'ev_settings_copyright_field\', \'evolutionary\', \'ev_settings_general_options\' );

add_settings_field( \'ev_settings_colors_field\', __( \'Theme Color\', \'evolutionary\' ), \'ev_settings_colors_field\', \'evolutionary\', \'ev_settings_general_options\' );

/**
 * Fields Callbacks
 */
function ev_settings_copyright_field() {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    //var_dump( $ev_options );
    ?>
    <input name="theme_evolutionary_options[copyright]" type="text" class="regular-text" value="<?php esc_attr_e( $ev_options[\'copyright\'] ); ?>"  />
<?php }
// Callback for colorscheme
function ev_settings_colors_field() {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    $color_schemes = ev_get_valid_color_schemes();

    echo \'<select name="theme_evolutionary_options[color_scheme]">\';

    foreach( $color_schemes as $value => $caption ) :
?>
        <option value="<?php echo $value; ?>" <?php selected( $value, $ev_options[\'color_scheme\'] ); ?>><?php echo $caption; ?></option>

<?php 
    endforeach;

    echo \'</select>\';
}

} // ev_register_options
add_action( \'admin_init\', \'ev_register_options\' );    

/**
 * Creating options page HTML output
 */
function ev_admin_options_page() { ?>

    <div class="wrap">
       <?php if ( isset( $_GET[\'settings-updated\'] ) ) {
                echo "<div class=\'updated\'><p>Theme settings updated successfully.</p></div>";
        } ?>
        <?php screen_icon(); ?>
        <h2><?php _e( \'Theme Options\', \'evolutionary\' ); ?></h2>
        <form action="options.php" method="post">
        <?php 
        settings_fields( \'theme_evolutionary_options\' );
        do_settings_sections( \'evolutionary\' );
        ?>

            <input name="submit" type="submit" class="button-primary" value="<?php _e( \'Save Settings\', \'evolutionary\' ) ?>" />
            <input name="reset" type="submit" class="button-secondary" value="<?php _e( \'Reset Settings\', \'evolutionary\' ) ?>" />
        </form>
    </div>
<?php }

function ev_options_validate( $input ) {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    $valid_input = $ev_options;

    $ev_options[\'color_scheme\'] = array_key_exists( $ev_options[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $ev_options[\'color_scheme\'] : \'blue\';
    $ev_options[\'copytright\'] = wp_kses_data( $ev_options[\'copyright\'] );

    return $valid_input;
}

3 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

@Mamaduka:

以下是您的设置未更新的原因。您的验证函数错误。也就是说:

function ev_options_validate( $input ) {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    $valid_input = $ev_options;

    $ev_options[\'color_scheme\'] = array_key_exists( $ev_options[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $ev_options[\'color_scheme\'] : \'blue\';
    $ev_options[\'copytright\'] = wp_kses_data( $ev_options[\'copyright\'] );

    return $valid_input;
}
您正在更新$ev\\U选项,但返回了$valid\\U输入。您需要更新$valid\\u输入,而不是$ev\\u选项。

请尝试以下操作:

function ev_options_validate( $input ) {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    $valid_input = $ev_options;

    $valid_input[\'color_scheme\'] = array_key_exists( $ev_options[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $ev_options[\'color_scheme\'] : \'blue\';
    $valid_input[\'copytright\'] = wp_kses_data( $ev_options[\'copyright\'] );

    return $valid_input;
}
看看这能不能解决你的问题?

编辑以添加:

验证函数中的下一个问题是,当需要将$输入与$valid\\u输入进行比较时,要将$ev\\u选项与自身进行比较。

而不是这样:

function ev_options_validate( $input ) {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    $valid_input = $ev_options;

    $valid_input[\'color_scheme\'] = array_key_exists( $ev_options[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $ev_options[\'color_scheme\'] : \'blue\';
    $valid_input[\'copytright\'] = wp_kses_data( $ev_options[\'copyright\'] );

    return $valid_input;
}
尝试以下操作:

function ev_options_validate( $input ) {
    $ev_options = get_option( \'theme_evolutionary_options\' );
    $valid_input = $ev_options;

    $valid_input[\'color_scheme\'] = array_key_exists( $input[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $input[\'color_scheme\'] : \'blue\';
    $valid_input[\'copytright\'] = wp_kses_data( $input[\'copyright\'] );

    return $valid_input;
}
具体而言,请更改以下内容:

$valid_input[\'color_scheme\'] = array_key_exists( $ev_options[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $ev_options[\'color_scheme\'] : \'blue\';
$valid_input[\'copytright\'] = wp_kses_data( $ev_options[\'copyright\'] );
对此:

$valid_input[\'color_scheme\'] = array_key_exists( $input[\'color_scheme\'], ev_get_valid_color_schemes() ) ? $input[\'color_scheme\'] : \'blue\';
$valid_input[\'copytright\'] = wp_kses_data( $input[\'copyright\'] );
然后,我认为,你的选择应该有效。

@TheDeadMedic:

设置API处理查看任何$POST数据。这不是问题所在。

关于init上的默认设置更新,下面是我的原始示例代码:

function oenology_options_init() {
     // set options equal to defaults
     global $oenology_options;
     $oenology_options = get_option( \'theme_oenology_options\' );
     if ( false === $oenology_options ) {
          $oenology_options = oenology_get_default_options();
     }
     update_option( \'theme_oenology_options\', $oenology_options );
}
// Initialize Theme options
add_action(\'after_setup_theme\',\'oenology_options_init\', 9 );
这是马马杜卡的密码:

function ev_options_init() {
    // Set options equal to defaults
    global $ev_options;
    $ev_options = ev_theme_settings_defaults();
    if ( false === $ev_options ) {
        $ev_options = ev_theme_settings_defaults();
    }
    update_option( \'theme_evolutionary_options\', $ev_options );
}

add_action( \'after_setup_theme\', \'ev_options_init\', 9 );
你看到区别了吗?从oenology\\u options\\u init()到ev\\u options\\u init()有一个关键的变化,即:

这是:

global $oenology_options;
$oenology_options = get_option( \'theme_oenology_options\' );
与此相比:

global $ev_options;
$ev_options = ev_theme_settings_defaults();
因此,在ev\\u options\\u init()中,由于发生了更改,函数实际上从未执行任何操作。

SO网友:TheDeadMedic

问题是他们正在被保存。。。只是你总是保存默认值!

$ev_options = ev_theme_settings_defaults();
if ( false === $ev_options ) {
    $ev_options = ev_theme_settings_defaults();
}
update_option( \'theme_evolutionary_options\', $ev_options );
它会在每次页面加载时运行(它与操作挂钩after_theme_setup).

更不用说if 声明毫无意义($ev_options 永远不会false).

您似乎还不太了解设置API。关键是你不需要自己保存选项,WordPress会帮你保存!

我肯定你已经查过了Settings API 虽然有点欠缺,但已经做到了。下面将更详细地介绍如何使用API将选项存储为数组。

SO网友:Chip Bennett

我想我明白问题所在了,马马库卡。

您应该在此处调用get\\u option(),而不是默认函数:

    /**
 * Setup initial theme options
 */
function ev_options_init() {
    // Set options equal to defaults
    global $ev_options;
    $ev_options = ev_theme_settings_defaults();
    if ( false === $ev_options ) {
        $ev_options = ev_theme_settings_defaults();
    }
    update_option( \'theme_evolutionary_options\', $ev_options );
}

add_action( \'after_setup_theme\', \'ev_options_init\', 9 );
这是:

global $ev_options;
$ev_options = ev_theme_settings_defaults();
应改为:

global $ev_options;
$ev_options = get_option( \'theme_evolutionary_options\' );
否则,您将按照HeadMedic的指示执行:在每个init上保存默认选项。

结束

相关推荐

如何获取我在Add_Options_Page中为我的页眉指定的标题

我有add_options_page(\'Post Products Settings\', \'Post Products Settings\', \'administrator\', \'pp_settings\', \'pp_settings_page\'); 无论如何,我可以得到在第一个参数中指定的内容,以便在我的页眉中使用?我知道我可以硬编码,但只是好奇