0

Settings API always been a little confusing for me.

I am trying to add a simple theme options page for a child theme I am developing. I guess I am doing something wrong that preventing the section being appearing. The settings_fields() is working fine though (printing all those hidden fields in the form), but the sections are not. Here is my code, please help me troubleshooting.

/********************************
    Add Theme Sub Menu Page
********************************/
function sr_theme_options_setup() {
    add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'srchild', 'ia_theme_page_settings' );
}
add_action( 'admin_menu', 'sr_theme_options_setup' );


/*************************************
    Generate Options Page HTML
*************************************/
function ia_theme_page_settings() {
?>
    <div class="wrap">
        <?php screen_icon('themes'); ?> <h2>Theme Settings</h2>
        <?php settings_errors( 'ia-settings-errors' ); ?>
        <form id="form-ia-options" action="options.php" method="post" enctype="multipart/form-data">
                <?php
                    settings_fields('ia_options');
                    do_settings_sections('ia_section_id');
                ?>

                <p class="submit">
                    <input name="ia_options[submit]" id="submit_options_form" type="submit" class="button-primary" value="<?php esc_attr_e('Save Settings', 'iathemes'); ?>" />
                    <input name="ia_options[reset]" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'iathemes'); ?>" />
                </p>
        </form>
    </div>
<?php
}


/******************************************
    Register Settings/Section/Fields
******************************************/
function iathemes_init() {
    register_setting( 'ia_options', 'ia_options', 'ia_options_validate' );

    // Add a form section for the Logo
    add_settings_section('ia_section_id', __( 'Theme Settings', 'iathemes' ), 'ia_section_callback', 'srchild');

    // Add Logo uploader
    add_settings_field('ia_options_logo',  __( 'Logo', 'iathemes' ), 'ia_options_logo', 'srchild', 'ia_section_id');
}
add_action( 'admin_init', 'iathemes_init' );

function ia_section_callback() {
    ?>
        <p><?php echo 'Manage Theme Settings'; ?></p>
    <?php
}

function ia_options_logo() {
    $options = get_option( 'ia_options' );
    ?>
        <input type="text" id="logo_url" name="ia_options[site_logo]" value="<?php echo esc_url( $options['site_logo'] ); ?>" />
        <input id="upload_logo_button" type="button" class="button" value="<?php _e( 'Upload Logo', 'iathemes' ); ?>" />
        <span class="description"><?php _e('Upload an image for the banner. Should be 300x40 pixels in size', 'iatheme' ); ?></span>
    <?php
}

Please tell me where I am doing wrong.

3

The settings API can be puzzling ;-)

Replace:

do_settings_sections( 'ia_section_id' );

with

do_settings_sections( 'srchild' );

to display your defined settings section.

The general usage is:

do_settings_sections( $page );

where:

$page (string) (required) The slug name of the page whose settings sections you want to output. This should match the page name used in add_settings_section(). Default: None

according to the Codex (here).

  • You're the man!! :) – Abhik Mar 14 '14 at 15:12

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.