Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress WordPress Settings API Creating a Theme Options Page Settings Quick Review of How to Add a Theme Setting

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

The correct code for saving the checkbox setting on the WordPress Settings API course.

Following on from my previous thread here, https://teamtreehouse.com/community/using-addthemepage yesterday thanks to this review video I was able to investigate and find the right code for saving the Checkbox setting.

It's actually available in the project downloads.

I followed the code exactly from the video and while the form field and label appeared, but the setting would not stay the same so it would clear the checkbox.

<?php  
function wpt_slideshow_checkbox_callback() {

    $options = get_option( 'wpt_show_slideshow' );  


    //$html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_settings[show_slideshow]" value="1"' . checked( 1, $options['show_slideshow'], false ) . '/>';
    $html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_show_slideshow" value="1"' . checked( 1, $options, false ) . '/>';
    $html .= '<label for="wpt_show_slideshow">Check to enable slideshow on the homepage</label>';

    echo $html;

}

?>

However if I check the downloads for the same callback function this code as an added condition statement that checks the status of the checkbox setting and remembers the last setting when saved.

So for clarity, this is the one to use.

<?php 
function wpt_slideshow_checkbox_callback() {

    $options = get_option( 'wpt_settings' );    
    if( !isset( $options['show_slideshow'] ) ) $options['show_slideshow'] = 0;

    $html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_settings[show_slideshow]" value="1"' . checked( 1, $options['show_slideshow'], false ) . '/>';
    $html .= '<label for="wpt_show_slideshow">Check to enable slideshow on the homepage</label>';

    echo $html;

}


 ?>

1 Answer

Konrad Pilch
Konrad Pilch
2,435 Points

Haven't done this yet, but thank you ( if it's good :D )!

Paul Jackson
Paul Jackson
115 Points

Hi Konrad, Just to let you know your solution to the Checkbox issue not saving it's state is the only one across the entire web i've found that works! Cheers.