【问题标题】:WordPress My theme options settings not savingWordPress 我的主题选项设置未保存
【发布时间】: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 88 Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 359 Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 361 这是来自 WP_DEBUG 的消息
  • 好的,您必须相应地处理此通知,主要使用isset() 并在 var 尚不存在时提供替代方案。你能在另一个你没有修改过的主题上测试我的代码吗?它对我有用,您的问题似乎出在您提供的示例代码之外的其他地方。

标签: php mysql wordpress


【解决方案1】:

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

  • 在未定义的情况下使用 $_POST['footer_text'](Praveen 的回答)
  • 使用已弃用的函数get_current_theme()
  • add_submenu_page() 中使用级别而不是能力

我将以下代码放入我的主题的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_ontheme_options_do_page() 覆盖,但您的示例代码并未显示它在其他地方使用。

【讨论】:

  • 感谢真正有效的解决方案 :) 非常感谢
  • @Mr.beginner,很高兴知道 :) 始终关注调试器通知和警告。那,以及适当的代码缩进,使生活变得更加轻松。祝你好运!
  • 是的,这确实更有帮助。 :) :D
  • 如果我想在下面的数组中添加更多选项,我应该添加什么来代替if( isset( $_POST['footer_text'] ) )if( isset( $_POST['footer_text'] ) ) { $new_values = array ( 'footer_text' =&gt; htmlentities( $_POST['footer_text'], ENT_QUOTES), ); update_option('new_theme_options', $new_values); $theme_options = $new_values; }
【解决方案2】:

请更改此代码:

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;
}

希望这会正常工作:)

【讨论】:

  • 使用这个插件 -- wordpress.org/plugins/advanced-custom-fields你可以在任何页面通过这个插件添加新的字段,请试试这个。
  • 我不想使用插件,我想在这里通过人们的帮助更正我的代码。
猜你喜欢
  • 2011-09-08
  • 2012-08-13
  • 2014-08-02
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
相关资源
最近更新 更多