主题选项页面不保存选项

时间:2017-06-29 作者:Makiomar

我正在尝试使用设置API来练习添加主题选项页面,但不幸的是,我根据codex编写的代码不能正常工作。当我测试代码时,没有生成错误,但是pe_bannar_heading 选项无法保存,请您帮助我找出以下代码中的问题所在

    <?php
    /*
    Plugin Name: Test option page
    Text Domain: test-option-page
    */

    function reg_settings(){
        register_setting(\'pe_theme_options\',\'pe_theme_options\');
        add_settings_section(\'pe_main_settings\',\'Main Settings\', \'\', __FILE__);
        add_settings_field(\'pe_bannar_heading\', \'Bannar Heading:\', \'pe_bannar_heading_setting\', __FILE__, \'pe_main_settings\');
    }

    add_action(\'admin_init\',\'reg_settings\');

    function pe_bannar_heading_setting(){ 

                $options = get_option(\'pe_theme_options\');?>
                <input name="pe_bannar_heading" type="text" value="<?php if(isset($options[\'pe_bannar_heading\'])) echo $options[\'pe_bannar_heading\'] ?>"/>

            <?php }

    function pe_add_menu_page(){
                add_options_page(\'Theme Options\', \'Theme Options\', \'administrator\', __FILE__ , \'display_options_page\');
            }

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

    function display_options_page(){

                $options = get_option(\'pe_theme_options\');?>
                <div class="wrap">
                    <h2>Yellow Blog Options</h2>
                    <form action="options.php" method="post" enctype="multipart/form-data">
                        <?php 
                            settings_fields(\'pe_theme_options\');
                            do_settings_sections(__FILE__);
                            submit_button( \'Save Settings\' );
                        ?>
                    </form>
                </div>  
    <?php   }

1 个回复
SO网友:Andrii Shekhirev

为了保持提交的值,需要将其保存到相应的选项数组中。为此,可以添加另一个函数(pe_save_settings() 在本例中)捕获表单提交并保存字段内容。以下是完整的工作代码:

<?php
/*
Plugin Name: Test option page
Text Domain: test-option-page
*/

function reg_settings(){
    register_setting(\'pe_theme_options\',\'pe_theme_options_item\');
    add_settings_section(\'pe_main_settings\',\'Main Settings\', \'\', __FILE__);
    add_settings_field(\'pe_bannar_heading\', \'Bannar Heading:\', \'pe_bannar_heading_setting\', __FILE__, \'pe_main_settings\');
}

add_action(\'admin_init\',\'reg_settings\');

function pe_bannar_heading_setting() { 
    $options = get_option(\'pe_theme_options_array\'); ?>
    <input id="pe_theme_options_item" name="pe_theme_options_item" type="text" value="<?php if ( isset( $options[\'pe_bannar_heading\'] ) ) { echo $options[\'pe_bannar_heading\']; } ?>"/>
    <?php 
}

function pe_add_menu_page(){
    add_options_page(\'Theme Options\', \'Theme Options\', \'administrator\', __FILE__ , \'display_options_page\');
}

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

function display_options_page() {
    $options = get_option(\'pe_theme_options_array\');?>
    <div class="wrap">
        <h2>Yellow Blog Options</h2>
        <form action="options.php" method="post" enctype="multipart/form-data">
            <?php 
            settings_fields(\'pe_theme_options\');
            do_settings_sections(__FILE__);
            submit_button( \'Save Settings\', \'primary\', \'pe_new_option\' );
            ?>
        </form>
    </div>  
<?php }

add_action( \'admin_init\', \'pe_save_settings\' );

function pe_save_settings() {
    if ( isset( $_POST[\'pe_new_option\'] ) ) {
        $new_value = trim( $_POST[ \'pe_theme_options_item\'] );
        $options_array = get_option(\'pe_theme_options_array\');
        $options_array[\'pe_bannar_heading\'] = $new_value;
        update_option( \'pe_theme_options_array\', $options_array );
    }
}

结束

相关推荐