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

Phil Purves
Phil Purves
9,827 Points

tidier code - same problem as stated earlier

I do not think the challenge is stated very clearly ... what am I missing?

  • any help appreciated
    Thanks, Phil P
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.');
  }

  if ( isset( $_POST['my_plugin_hidden_field_submitted'] ) ) {

    $hidden_field = esc_html( $_POST['my_plugin_hidden_field_submitted'] );

    if( $hidden_field == 'Y' ) {

      $my_plugin_username = esc_html( $_POST['my_plugin_username'] );

    }

  }


}

?>

9 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

Hi again Phil,

As mentioned in the previous thread, the extra if statement you've written for $hidden_field is unnecessary. Based on your code, all you need is:

<?php

function my_plugin_options_page() {

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

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

}

?>

However this doesn't pass because the field name in your $_POST value doesn't match what was asked in the previous question (it should be $_POST['my_plugin_hidden_field']). If you make the change, the challenge passes. So the problem really is with the first part of the challenge as it shouldn't have allowed you to move onto the next step..

Phil Purves
Phil Purves
9,827 Points

Thanks - oops I was responding in the previous conversation ... here was my last comment -

Hi again - have tried all alterations suggested to no avail. Last warning message: "Bummer! You should pass the posted form element my_plugin_username into esc_html in order to sanitize it" The challenge does not instruct you to restate the form input/method etc, and in Zac's worked example he does that in a separate file under a postbox class.

Still bamboozled by this! - but trying again, just as you suggest - Phil

Joel Bardsley
Joel Bardsley
31,249 Points

Sometimes the "Bummer..." message given isn't appropriate in some challenges in certain situations, so it's easy to get led astray. You'll have to take my word for it that there's nothing wrong with your esc_html line.

Just to double check, with my suggestions you should have code that's something like:

<?php

function my_plugin_options_page() {

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

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

}

?>

I've just tried the above code in the challenge and it lets you get to challenge 3 of 3. If you need more information on why this code passes, please let me know as it wouldn't be helpful to just give you the answer without you fully understanding why it passes.

Phil Purves
Phil Purves
9,827 Points

failed as stated above... code submitted as follows (following your lead): <?php function my_plugin_options_page() { if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to access this page.'); } if ( isset( $_POST['my_plugin_hidden_field'] ) ) {
$hidden_field = esc_html( $_POST['my_plugin_hidden_field'] );
if( $hidden_field == 'Y' ) {
$my_plugin_username = esc_html( $_POST['my_plugin_username'] );
}
} } ?>

Phil Purves
Phil Purves
9,827 Points

This time without trying to condense:

<?php

function my_plugin_options_page() {

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

if ( isset( $_POST['my_plugin_hidden_field'] ) ) {

$hidden_field = esc_html( $_POST['my_plugin_hidden_field'] );

if( $hidden_field == 'Y' ) {

  $my_plugin_username = esc_html( $_POST['my_plugin_username'] );

}

}

}

?>

Phil Purves
Phil Purves
9,827 Points

there's obviously a problem with pasting code - can I attach an image? how? Phil

Phil Purves
Phil Purves
9,827 Points

Thanks for your perseverance Joel

Interesting! I was using Firefox ... then pasted the exact same code into Chrome - and it passed! Food for thought.

Phil

Joel Bardsley
Joel Bardsley
31,249 Points

No worries, I wish my Chrome had similar challenge-fixing powers! Keep at it, Zac's a great teacher so you'll be a WordPress plugin master in no time.

Phil Purves
Phil Purves
9,827 Points

Sorry again... my mistake. Chrome still fails me on task 2 of 3... it was just passing me on task 1. Can I send a screenshot so you know just what I'm submitting? I incorrect I wonder why you get a pass with the same code.

Joel Bardsley
Joel Bardsley
31,249 Points

Upload a screenshot to imgur and paste the link into here and I'll take a look.

Phil Purves
Phil Purves
9,827 Points

imgur upload here: http://imgur.com/M0bPH10

Are you saying you can get through to stage three with this? Thanks for your trouble - Phil

Joel Bardsley
Joel Bardsley
31,249 Points

No, you can get through to stage three with the code I pasted earlier:

<?php

function my_plugin_options_page() {

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

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

}

?>

As mentioned earlier, the extra if statement for the hidden field isn't necessary. Although Zac showed the process in the previous video, at no point in the questions for the first two challenges does it say to check the value of the hidden field. As you cannot see the html of the form in the challenge, it can't be assumed that it uses a value of "Y".

Phil Purves
Phil Purves
9,827 Points

Got it! - once again, many thanks Joel ... I would offer a 'best answer' tick but those options are only appearing for my replies for some reason. All Best