【发布时间】:2014-03-17 10:16:38
【问题描述】:
我刚刚创建了一个简单的主题选项页面,该页面运行良好,并且在按下保存选项后也保存了。
但是当我从主题选项页面转到其他地方并返回主题选项页面时,我保存的设置就会消失,每当我进入主题选项页面时,我都必须再次更改。
这是我的代码
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
}
【问题讨论】:
-
您在哪里使用此代码?如果禁用所有插件会怎样?您是否在干净的主题中使用代码进行了测试?有来自
WP_DEBUG的消息吗? -
是的,我的主题很干净,我在 funcions.php 中使用它,我没有太多插件,我也在 localhost 上使用它。我没有启用 WP_DEBUG。
-
Notice: Undefined variable: defaults in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 88Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 359Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 361这是来自 WP_DEBUG 的消息 -
好的,您必须相应地处理此通知,主要使用
isset()并在 var 尚不存在时提供替代方案。你能在另一个你没有修改过的主题上测试我的代码吗?它对我有用,您的问题似乎出在您提供的示例代码之外的其他地方。