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 Coding Out a Theme Options Page Setting Form

Jerry Schrader
Jerry Schrader
4,396 Points

Maybe I am mistaken, but it seems like steps were skipped, where html was added for the form

In stage 2 there is html added to your presentation that I did not see added, unless I missed something.

3 Answers

Patrick O'Dacre
Patrick O'Dacre
15,471 Points

You are correct. I noticed the same thing.

Here's the code as best I can make it out:

Placed in the checkbox callback function:

$option = get_option( 'wpt_show_slideshow' );

$html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_show_slideshow" value="1"' . checked( 1, $option, false ) . '/>';

$html .= '<label for="wpt_show_slideshow">Check to enable slideshow on the homepage</label>';

echo $html;

Patrick O'Dacre
Patrick O'Dacre
15,471 Points
// get_option is the WordPress function to grab a value from the WordPress options table in the database. 
// 'wpt_show_slideshow' is the specific database key we're using to grab our specific option value.
$option = get_option( 'wpt_show_slideshow' );

// Setting up our form field referencing wp_show_slideshow key, ensures the value being submitted from this field is stored in the wp_show_slideshow row in the options table.
// Here we're creating a variable called 'html' and we're adding a long string of html code to that variable.
$html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_show_slideshow" value="1"' . checked( 1, $option, false ) . '/>';

// here the 'period' before the 'equals' sign adds this new string to the previous one.
$html .= '<label for="wpt_show_slideshow">Check to enable slideshow on the homepage</label>';

// here we echo out the combined strings of html.
echo $html;

As you play with the options table, I recommend you open the database in your IDE (like PhpStorm) or with a program meant for browsing databases (phpMyAdmin works just fine) to see how your code is actually affecting the database. You can view the options table in the WordPress database and see your key 'wpt_show_slideshow' there with your values in it.

But there is no explanation about this code.

$option = get_option( 'wpt_show_slideshow' );

$html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_show_slideshow" value="1"' . checked( 1, $option, false ) . '/>';

$html .= '<label for="wpt_show_slideshow">Check to enable slideshow on the homepage</label>';

echo $html;