1

The entire day today I remained frustrated with few things. I am learning WordPress setting API from a Video Course offered by Team Tree House. I was watching Videos and writing the codes ditto, but have an issue at one point.

I am reproducing functions.php file code here

function wpt_theme_init() {
    register_setting( 'wptsettings-group', 'wpt_settings' );
    add_settings_section(
        'wpt_slideshow_section',
        'Slideshow Settings',
        'wpt_slideshow_section_callback',
        'general'
    );
    add_settings_field(
        'wpt_slideshow_checkbox',
        'Show slideshow on homepage',
        'wpt_slideshow_checkbox_callback',
        'general',
        'wpt_slideshow_section'
    );
}

// Create Theme Options Page
function wpt_add_theme_page() {

    add_theme_page(
        __('Theme Options', 'wpsettings'),
        __('Theme Options', 'wpsettings'),
        'edit_theme_options',
        'wptsettings',
        'wpt_theme_options_page'
    );

}
add_action('admin_menu', 'wpt_add_theme_page');
function wpt_theme_options_page() {
?>
<div class="wrap">

    <h2>Theme Options - <?php echo get_current_theme(); ?></h2>
    <form method="post" action="options.php">
    <?php
        settings_fields( 'wptsettings-group' );
        do_settings_sections( 'wptsettings' );
        submit_button();
    ?>

</div>

<?php
}


// Enqueue theme styles
function wpt_theme_styles() {

  wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );

}
add_action( 'wp_enqueue_scripts', 'wpt_theme_styles' );

?>

I have crossed checked multiple times, but the issue is that I am unable to see this in my admin page. I should gete thisenter image description here

But I am getting this.enter image description here

Couldn't find since morning hats going wrong.

In short this part is not completely working →

<div class="wrap">

    <h2>Theme Options - <?php echo get_current_theme(); ?></h2>
    <form method="post" action="options.php">
    <?php
        settings_fields( 'wptsettings-group' );
        do_settings_sections( 'wptsettings' );
    ?>

</div>
1

You are Missing few Things. First of all hook the function wpt_theme_init with admin_init or similar one. otherwise your function will not execute. example below

add_action( 'admin_init', 'wpt_theme_init' );

add_settings_section and add_settings_field take 4th argument as a $page parameter. which is wptsettings in your case. remember? you have named your page wptsettings in add_theme_page ( 4th paramater ). so all these values must match.

Incorrect

add_settings_section(
    'wpt_slideshow_section',
    'Slideshow Settings',
    'wpt_slideshow_section_callback',
    'general'
);
add_settings_field(
    'wpt_slideshow_checkbox',
    'Show slideshow on homepage',
    'wpt_slideshow_checkbox_callback',
    'general',
    'wpt_slideshow_section'
);

Correct

add_settings_section(
    'wpt_slideshow_section',
    'Slideshow Settings',
    'wpt_slideshow_section_callback',
    'wptsettings'
);
add_settings_field(
    'wpt_slideshow_checkbox',
    'Show slideshow on homepage',
    'wpt_slideshow_checkbox_callback',
    'wptsettings',
    'wpt_slideshow_section'
);

Lastly you've not defined callback functions for section and field. this is required.

wpt_slideshow_section_callback
wpt_slideshow_checkbox_callback

Section and field callback functions.

function wpt_slideshow_section_callback() {}
function wpt_slideshow_checkbox_callback() {

$setting = esc_attr( get_option( 'wpt_settings' ) );
?>
<input type="checkbox" name="wpt_settings" value="1"<?php checked( 1 == $setting ); ?> />
<?php

}

The complete correct code for your admin page displaying checkbox.

add_action( 'admin_init', 'wpt_theme_init' );
function wpt_theme_init() {
    register_setting( 'wptsettings-group', 'wpt_settings' );
    add_settings_section(
        'wpt_slideshow_section',
        'Slideshow Settings',
        'wpt_slideshow_section_callback',
        'wptsettings'
    );
    add_settings_field(
        'wpt_slideshow_checkbox',
        'Show slideshow on homepage',
        'wpt_slideshow_checkbox_callback',
        'wptsettings',
        'wpt_slideshow_section'
    );
}
function wpt_slideshow_section_callback() {}
function wpt_slideshow_checkbox_callback() {
$setting = esc_attr( get_option( 'wpt_settings' ) );
?>
<input type="checkbox" name="wpt_settings" value="1"<?php checked( 1 == $setting ); ?> />
<?php
}
// Create Theme Options Page
function wpt_add_theme_page() {

    add_theme_page(
        __('Theme Options', 'wpsettings'),
        __('Theme Options', 'wpsettings'),
        'manage_options',
        'wptsettings',
        'wpt_theme_options_page'
    );

}
add_action('admin_menu', 'wpt_add_theme_page');
function wpt_theme_options_page() {
?>
<div class="wrap">

    <h2>Theme Options - <?php echo wp_get_theme(); ?></h2>
    <form method="post" action="options.php">
    <?php
        settings_fields( 'wptsettings-group' );
        do_settings_sections( 'wptsettings' );
        submit_button();
    ?>

</div>

<?php
}

Let me know if its working or need more help?

  • Oh Boss You made my day. You could not believe how much frustrated and depressed I was for the entire day. as soon as I changed 'general', to 'wptsettings' everything seems to be in order. Thank you so much sir. – The WP Novice Dec 28 '16 at 12:30
  • glad to hear that. you're welcome – Anwer AR Dec 28 '16 at 18:37

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.