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 How to Build a WordPress Plugin Building a WordPress Plugin Settings Page Admin Area Forms in WordPress

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
15,328 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'];
}