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 trial

WordPress How to Build a WordPress Plugin Building a WordPress Plugin Settings Page Admin Area Forms in WordPress

Luke Lee
Luke Lee
7,577 Points

Why do we need hidden_field in the form?

Why do we need the following code?

if(isset($_POST['wptreehouse_form_submitted'])){
        $hidden_field = esc_html($_POST['wptreehouse_form_submitted']);
        if($hidden_field == 'Y'){
        $wptreehouse_username = esc_html($_POST['wptreehouse_username']);    
        }

Why cant we just do:

if(isset($_POST['wptreehouse_username'])){
$wptreehouse_username = esc_html($_POST['wptreehouse_username']);    
}

1 Answer

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

When we process forms from within the functions.php file we tie the related functions the WordPress init hook. This way as soon as WordPress starts spitting out our theme, it's ready to handle the form submission.

Giving each form we create a unique hidden field and related value let's us setup "listeners" of sorts to borrow from the JS world that will be called when the related form is submitted. It becomes more clear as you add more functions that tie to form submissions.