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

fakrulislam
fakrulislam
5,090 Points

how to check my_plugin_hidden_field has been submitted or not using isset?

I am trying to check my_plugin_hidden_field has been submitted or not using the isset() function within if statement. For example: $hidden_field = $POST['my_plugin_hidden_field']; if( isset($hidden_field) ){

}

Showing an error message to me that: in order to check that condition I must need to use the isset() function which I used already.

Thanks for your help!

plugin.php
<?php

function my_plugin_options_page() {

    if (!current_user_can('manage_options')) {
        wp_die('You do not have sufficient permissions to access this page.');
    }

  $hidden_field = $POST['my_plugin_hidden_field'];
    if ( isset( $hidden_field ) ){

  }



}

?>

1 Answer

Luke Towers
Luke Towers
18,572 Points

First problem, you're using $POST, not $_POST which is the proper global variable to use to access variables in POST requests to your PHP script.

You need to check if $POST['my_plugin_hidden_field']; is set, not if your variable is set. Just by putting the line

<?php
$hidden_field = $_POST['my_plugin_hidden_field'];

you have set the $hidden_field variable. What you are trying to do is determine if the my_plugin_hidden_field variable was set in the POST request sent to your PHP script. In order to do that, you must do the following:

<?php
if (isset($_POST['my_plugin_hidden_field'])) {
    $hidden_field = $_POST['my_plugin_hidden_field'];
}