Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJonathan Grieve
Treehouse Moderator 91,253 PointsThe 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
2,435 PointsHaven't done this yet, but thank you ( if it's good :D )!
Paul Jackson
115 PointsPaul Jackson
115 PointsHi 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.