无法输出DO_SETTINGS_SECTIONS。我不明白为什么

时间:2014-05-03 作者:Dudo1985

我有以下代码:

/* Hook to admin_menu the yasr_add_pages function above */
add_action(\'admin_menu\', \'yasr_add_pages\');

function yasr_add_pages() {

    //Add Settings Page
    add_options_page( 
                    \'Yet Another Stars Rating: Settings\',               //Page Title
                    __(\'Yet Another Stars Rating: Settings\', \'yasr\'),   //Menu Title
                    \'manage_options\',                                   //capablity
                    \'yasr_settings_page\',                               //menu slug
                    \'yasr_settings_page_content\'                        //The function to be called to output the content for this page.
                    ); 

}

/* Settings Page Content */
function yasr_settings_page_content() {
    if ( !current_user_can( \'manage_options\' ) )  {
        wp_die( __( \'You do not have sufficient permissions to access this page.\', \'yasr\' ) );
    }
    include (YASR_ABSOLUTE_PATH . \'/yasr-settings-page.php\');

} //End yasr_settings_page_content
这是yasr设置页面中的内容:

<div class="wrap">
    <h2>Settings API Demo</h2>
    <form action="options.php" method="post">
        <?php 
            settings_fields( \'yasr_multi_form\' ); 
            do_settings_sections( \'yasr_settings_page\' );
            submit_button( \'Salva\' );
        ?>
    </form>
</div>

<?php

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

function yasr_multi_form_init() {
    register_setting ( 
                \'yasr_multi_form\', // A settings group name. Must exist prior to the register_setting call. This must match the group name in settings_fields()
                \'yasr_multi_form_data\' //The name of an option to sanitize and save. 
    );

add_settings_section( \'yasr_section_id\', \'Gestione Multi Set\', \'yasr_section_callback\', \'yasr_settings_page\' );
add_settings_field( \'yasr_field_name_id\', \'Nome Set\', \'yasr_nome_callback\', \'yasr_settings_page\', \'yasr_section_id\' );
}

function yasr_section_callback() {
    echo "Descrizione sezione";
}

function yasr_nome_callback() {
    $option = get_option( \'yasr_multi_form_data\' );
    $name = esc_attr( $option[\'name\'] );
    echo "<input type=\'text\' name=\'yasr_multi_form_data[name]\' value=\'\' />";
}
设置页面输出settings_field 函数(我可以看到源代码中隐藏的输入类型)和提交按钮,但我无法呈现do_settings_section 我真的不明白为什么。有什么建议吗?

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

据我所见,您正在混合输出和代码,这意味着要准备输出。此外,准备工作是在输出开始之后进行的。我觉得很奇怪。

在这里,我更新了您的代码,它工作正常:

/* Hook to admin_menu the yasr_add_pages function above */
add_action( \'admin_menu\', \'yasr_add_pages\' );

function yasr_add_pages() {

    //Add Settings Page
    add_options_page(
        \'Yet Another Stars Rating: Settings\', //Page Title
        __( \'Yet Another Stars Rating: Settings\', \'yasr\' ), //Menu Title
        \'manage_options\', //capability
        \'yasr_settings_page\', //menu slug
        \'yasr_settings_page_content\' //The function to be called to output the content for this page.
    );

}

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

function yasr_multi_form_init() {
    register_setting(
        \'yasr_multi_form\', // A settings group name. Must exist prior to the register_setting call. This must match the group name in settings_fields()
        \'yasr_multi_form_data\' //The name of an option to sanitize and save.
    );

    add_settings_section( \'yasr_section_id\', \'Gestione Multi Set\', \'yasr_section_callback\', \'yasr_settings_page\' );
    add_settings_field( \'yasr_field_name_id\', \'Nome Set\', \'yasr_nome_callback\', \'yasr_settings_page\', \'yasr_section_id\' );
}

function yasr_section_callback() {
    echo "Descrizione sezione";
}

function yasr_nome_callback() {
    $option = get_option( \'yasr_multi_form_data\' );
    $name   = esc_attr( $option[\'name\'] );
    echo "<input type=\'text\' name=\'yasr_multi_form_data[name]\' value=\'\' />";
}

/* Settings Page Content */
function yasr_settings_page_content() {
    if ( ! current_user_can( \'manage_options\' ) ) {
        wp_die( __( \'You do not have sufficient permissions to access this page.\', \'yasr\' ) );
    }
    ?>
    <div class="wrap">
        <h2>Settings API Demo</h2>

        <form action="options.php" method="post">
            <?php
            settings_fields( \'yasr_multi_form\' );
            do_settings_sections( \'yasr_settings_page\' );
            submit_button( \'Salva\' );
            ?>
        </form>
    </div>

<?php
} //End yasr_settings_page_content
enter image description here

SO网友:Nick Young

因此,这里的问题实际上与您注册设置或输出设置的方式无关。我发现归根结底是对行动顺序的误解。

尝试创建一个包含所有设置代码的文件,并使用“init”操作调用该文件。您正在调用的“admin\\u init”操作是正确的,但只是在运行admin\\u init之后才调用它。

结束

相关推荐

如何创建多个顶级管理菜单页面以将数据提交到Options表?

我试图在wordpress主题中创建多个顶级选项页面。但现在我只能保存一个的数据。如何在不必创建选项卡式面板的情况下为两个人保存数据?// create custom plugin settings menu add_action(\'admin_menu\', \'baw_create_menu\'); function baw_create_menu() { //create new top-level menu add_menu_page(\'B