1

I've this code:

/* 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

and, this is the content in yasr-settings-page:

<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='' />";
}

The settings page output settings_field function (I can see the input type hidden on my source) and the submit button, but I can't render the do_settings_section and I really can't understand why. Any suggestions?

2

From what I see, you are mixing output and the code, which is meant to prepare that output. More, the preparation is coming after the output started. Looks weird to me.

Here I updated your code, and it works OK:

/* 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

  • Why?? Please add an explanation to why your code should be used and why it should work. Also add an explanation to what the OP did wrong. This type of answers is not accepted. You are just simply dumping code here. Please edit your answer. – Pieter Goosen May 4 '14 at 13:58
  • @PieterGoosen - Sorry. The OP asked the same question on G+, and we had a discussion there. You are right, and I updated my answer. Thank you! – tivnet May 4 '14 at 15:19
  • Hi tivnet, yeah, your code just works. What I don't understand it's why I can't use the include: I'd like to put the form in a another file, but if I cut and paste the form in a new one file i can't output again the do_settings_sections – Dudo1985 May 4 '14 at 18:36
  • Yes, but put there only the form. Not the code creating section. Your include is within a hook, so it is included too late to run another hooks. – tivnet May 4 '14 at 19:54
0

So the problem here isn't actually anything to do with the way you are registering settings or outputting them. What I found it came down to was a mistiming on the order of actions.

Try making a file to hold all of your settings code and call that file with an action of 'init'. The 'admin_init' action you are calling is correct, but it is just that it is being called after the admin_init has already been run.

  • Looking at the codex codex.wordpress.org/Function_Reference/do_settings_sections the do_settings_section function take as unique parmater the page and not the id section. I've hovewer tried to use the section id like you said but it doesn't work – Dudo1985 May 3 '14 at 18:37
  • Oh you are correct, sorry about that. Setting up a blank project so I can help you get this working. Give me a few and I will update my answer. – Nick Young May 3 '14 at 18:48
  • Updated answer to what I found was happening when I tested it out. – Nick Young May 3 '14 at 19:01
  • I've tried to put all the code in just one page, but still doesn't work. Hovewer, there is just one 'admin_menu' action, and just one admin_init action – Dudo1985 May 4 '14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.