WordPress setting API strucked

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?

Leave a Comment