WordPress设置API未保存选项

时间:2012-08-23 作者:Nistor Alexandru

我刚开始学习Wordpress设置API,事情并不像我第一次想到的那么简单。我创建了一个部分和两个字段,这些字段似乎没有保存数据。每次单击字段时,数据都会被删除。这是我的代码:

<?php
    function thanathos_theme_menu(){
        add_menu_page(
                        \'Thanathos\',
                        \'Thanathos\',
                        \'administrator\',
                        \'thanathos_menu_id\',
                        \'thanathos_menu_display_callback\'
                     );
        add_submenu_page(
                        \'thanathos_menu_id\',
                        \'Blog Page\',
                        \'Blog Page\',
                        \'administrator\',
                        \'thanathos_social_logo_options\',
                        \'thanathos_menu_display_callback\'
                     );
    }
    add_action(\'admin_menu\' , \'thanathos_theme_menu\');

    function thanathos_menu_display_callback(){
?>      <div class="wrap">
            <div id="icon-themes" class="icon32"></div>
            <h2>Thanathos Theme Options</h2>
            <?php settings_errors(); ?>
            <form method="post" action="options.php">
                 <?php settings_fields( \'thanathos_social_logo_options\' ); ?> 
                 <?php do_settings_sections( \'thanathos_social_logo_options\' ); ?>
                <?php  submit_button(); ?>
            </form>  
        </div>
<?php }?>
<?php
    function thanathos_initialize_social_logo_options(){
        if(false == get_option(\'thanathos_social_logo_options\')){
            add_option(\'thanathos_social_logo_options\');
        }       
        add_settings_section(
                    \'logo_social_section\',
                    \'Logo & Social Options\',
                    \'logo_social_section_callback\',
                    \'thanathos_social_logo_options\'
                );
        add_settings_field(
                    \'logo_field\',
                    \'Logo URL\',
                    \'logo_field_callback\',
                    \'thanathos_social_logo_options\',
                    \'logo_social_section\'
                );
        add_settings_field(
                    \'facebook_field\',
                    \'Facebook URL\',
                    \'facebook_field_callback\',
                    \'thanathos_social_logo_options\',
                    \'logo_social_section\'
                );
        register_setting(
                    \'thanathos_social_logo_options\',
                    \'thanathos_social_logo_options\'
                );
    }
    add_action( \'admin_init\', \'thanathos_initialize_social_logo_options\' );
    function logo_social_section_callback(){}

    function logo_field_callback(){
        $option = get_option(\'thanathos_social_logo_options\'); 
        echo \'<input type="text" id="logo" name="thanathos_social_logo_options[logo_field]" value="\' . $options[\'logo_field\'] . \'" />\';
    }
     function facebook_field_callback(){
        $option = get_option(\'thanathos_social_logo_options\'); 
        echo \'<input type="text" id="facebook" name="thanathos_social_logo_options[facebook_field]" value="\' . $options[\'facebook_field\'] . \'" />\';
    }
?>

1 个回复
最合适的回答,由SO网友:hacksy 整理而成

实际上这个错误很小,在你写的回调中

function logo_field_callback(){
        $option = get_option(\'thanathos_social_logo_options\'); 
        echo \'<input type="text" id="logo" name="thanathos_social_logo_options[logo_field]" value="\' . $options[\'logo_field\'] . \'" />\';
    }
     function facebook_field_callback(){
        $option = get_option(\'thanathos_social_logo_options\'); 
        echo \'<input type="text" id="facebook" name="thanathos_social_logo_options[facebook_field]" value="\' . $options[\'facebook_field\'] . \'" />\';
    }
正如您所看到的,您加载为$选项,但打印为$选项,这是您唯一的问题

结束

相关推荐