WordPress我的主题选项设置不保存

时间:2014-03-17 10:16:39

标签: php mysql wordpress

我刚创建了一个简单的主题选项页面,该页面工作正常,并在按下保存选项后保存。

但是当我从主题选项页面转到其他地方并回到主题选项页面时,我保存的设置就会消失,每当我进入主题选项页面时我都必须再次更改。

这是我的代码

add_action( 'admin_menu', 'theme_options_add_page' );

if ( get_option('new_theme_options')) {
$theme_options = get_option('new_theme_options');
} else {
add_option('new_theme_options', array (
    'sidebar2_on' => true,
    'footer_text' => ''
));
$theme_options = get_option('new_theme_options');

}

function theme_options_add_page() {
add_submenu_page( 'themes.php', 'My Theme Options', 'Theme Options', 8,    'themeoptions', 'theme_options_do_page' );
}

function theme_options_do_page() {
global  $theme_options;



    $new_values = array (
        'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
        );
update_option('new_theme_options', $new_values);

$theme_options = $new_values;

?>
    <div class="wrap">
    <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme   Options', 'responsivetheme' ) . "</h2>"; ?>




 <form method="post" action="themes.php?page=themeoptions">

 <label for="footer_text">Footer Text:</label>
 <input id="footer_text" type="text" name="footer_text" value="<?php echo   $theme_options['footer_text']; ?>" />

    <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
        </p>
    </form>
</div>
 <?php
}

2 个答案:

答案 0 :(得分:1)

请更改此代码:

if($_POST['footer_text']) {
$new_values = array (
    'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
    );
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
}

希望这样可以正常工作:)

答案 1 :(得分:1)

@Praveen答案是正确的,但为了完整起见,我将发布我测试的完整代码。请注意,您应该始终在启用WP_DEBUG的情况下进行开发。它显示了代码的三个问题:

  • 使用$_POST['footer_text']而未定义(Praveen的回答)
  • 使用已弃用的函数get_current_theme()
  • add_submenu_page()
  • 中使用Level而不是Capability

我将以下代码放入我的主题functions.php中,并且运行正常:

if ( get_option('new_theme_options')) {
    $theme_options = get_option('new_theme_options');
} else {
    $theme_options = array (
        'sidebar2_on' => true,
        'footer_text' => ''
    );
    add_option( 'new_theme_options', $theme_options );
}

add_action( 'admin_menu', 'theme_options_add_page' );

function theme_options_add_page() {
    add_submenu_page( 
        'themes.php', 
        'My Theme Options', 
        'Theme Options', 
        'add_users',    
        'themeoptions', 
        'theme_options_do_page' 
    );
}

function theme_options_do_page() {
    global  $theme_options;
    if( isset( $_POST['footer_text'] ) ) {
        $new_values = array (
            'footer_text' => htmlentities( $_POST['footer_text'], ENT_QUOTES),
        );
        update_option('new_theme_options', $new_values);
        $theme_options = $new_values;
    } 
    ?>
    <div class="wrap">
        <?php screen_icon(); echo "<h2>" . wp_get_theme() . __( ' Theme   Options', 'responsivetheme' ) . "</h2>"; ?>
        <form method="post" action="themes.php?page=themeoptions">
            <label for="footer_text">Footer Text:</label>
            <input id="footer_text" type="text" name="footer_text" value="<?php echo   $theme_options['footer_text']; ?>" />
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
            </p>
        </form>
    </div>
    <?php
}

我能看到的唯一问题是sidebar2_on中覆盖的选项值theme_options_do_page(),但您的示例代码未显示在其他地方使用过。