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
}
布拉索菲洛

@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_on正在中覆盖的选项值theme_options_do_page(),但是您的示例代码并未显示该值在其他地方使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我的Wordpress主题设置错误

来自分类Dev

我的设置未保存变量

来自分类Dev

WordPress添加主题选项

来自分类Dev

WordPress添加主题选项

来自分类Dev

如何从我的Wordpress插件中保存设置?

来自分类Dev

WordPress自定义选项页未显示设置字段

来自分类Dev

WordPress的功能主题选项格式

来自分类Dev

关于Wordpress中的主题选项

来自分类Dev

我的WordPress主题错误

来自分类Dev

当我在Windows 7中更改主题时,未出现“未保存的主题”

来自分类Dev

共享首选项未保存我的数据

来自分类Dev

圆角和圆角矩形边框设置未保存在主题中

来自分类Dev

我的WordPress主题的字符问题

来自分类Dev

Wordpress 让 Ajax 调用我的主题

来自分类Dev

主题选项中的标题未显示

来自分类Dev

WinForm设置未保存

来自分类Dev

显示设置未保存

来自分类Dev

PreferenceActivity设置未保存

来自分类Dev

显示设置未保存

来自分类Dev

声音设置未保存

来自分类Dev

功能未显示在子主题WordPress中

来自分类Dev

WordPress主题-删除未使用的CSS

来自分类Dev

图像未显示在WordPress主题中

来自分类Dev

功能未显示在子主题WordPress中

来自分类Dev

如何为WordPress主题开发设置git

来自分类Dev

WordPress文件未在主题设置中上传

来自分类Dev

我的照片未保存

来自分类Dev

WordPress插件WooCommerce,自定义支付网关设置未保存

来自分类Dev

我的wordpress主题出现字符问题