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

I'm totally confused in this challenge, please any help.

Below the first if statement, write a conditional statement to test if the my_plugin_hidden_field input field has been submitted. My answer is wrong. Any help how to resolve it?<?php

function my_plugin_options_page() {

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

}

global $plugin_url;

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

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

if( $my_plugin_hidden_field == 'Y' ) {

        $my_plugin_hidden_field_username = esc_html( $_POST['my_plugin_hidden_fiedl_username'] );



  }

}

?>

16 Answers

This passed:

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_form_submitted'] ) ) 
  $hidden_field = esc_html( $_POST['my_plugin_hidden_field_form_submitted'] );
}

}

?>

lyonel scapino
lyonel scapino
14,191 Points

The treehouse editor is not fully stable.

Ed,

Let's take a look at all three steps in the code challenge carefully:

Challenge 1 is having you to write an IF statement to see if the input field my_plugin_hidden_field was submitted. You almost had it right but take a look at what the IF statement is checking; it is $_POST but the field you are checking is wrong.

Challenge 2 is asking you to create a variable called my_plugin_username inside of the IF statement you created and assign it to the HTML escaped value of the my_plugin_username input field that got sent with my_plugin_hidden_field. The function is correct, just not what you are passing.

Challenge 3 is asking you to 'require' a file named page-wrapper.php (which is located in the includes folder) after the IF statement you created.

Once you do these three code challenges that way I specified, you will have satisfied the requirements for that code challenge. Make sure you read everything carefully and try not to over-think the instructions. If you get stuck just re-watch the previous videos. Hopefully this will assist you in understanding how to do the code challenges. Best of luck.

Cheers!

Ed,

I'm an advanced programmer when it comes to PHP so when TeamTreehouse puts a PHP course up I just review the videos and do the quizzes/code challenges. Whenever I respond to questions like this I go back and redo the code challenges to make sure I'm answering your questions correctly. When redoing the challenges, doing it like I specified passes all of the challenges. Now keep in mind that the code challenges are asking for specific information and is sometimes expecting certain pieces of code that, if you don't have that specific code, will fail you no matter how syntactically right you may be.

  1. The challenge is asking to you verify that the form was submitted and the field in question was submitted. These two questions can be answered by checking to see if the field in question my_plugin_hidden_field is present as the field can't be set if the form wasn't submitted. That's all the challenge is asking you and if you have any other pieces of information, (you should still have what was already there from the beginning) you should remove it at this point.
  2. This challenge is asking you to create a variable my_plugin_username and assign it the field of the same name that was submitted with the form $_POST['my_plugin_username'] Usually when you submit a form for processing, the processing script, it's good practice to take the form field name and assign it's value to a variable in the processing script of the same name. Now because you should never trust anything anyone submits in a form, you surround the POST variable in the function esc_html(). You will put all of this inside of the IF statement you created as there is no need to do this if the form wasn't submitted.
  3. After the IF statement you created, you are to "require" the file page-wrapper.php that is located in the includes directory.

So if you do all this, you should only have the IF statement conditional plus the code that was there from the start for you to pass challenge 1. Then you should have all that plus the new variable you created and the html escaped value of the form field of the same name that you assign to the new variable and put all that inside of the IF statement for challenge 2 to pass. Then finally you should have all that plus the require statement to require the page-wrapper.php file underneath the IF statement you created in challenge 1 for challenge 3 to pass.

Start the challenge from the beginning and work your way through all 3 challenges and you should pass them all. I hope I have cleared up any confusion you may have gotten from doing this code challenge thus far. Tell me how it goes.

Cheers!

Ed,

Your IF statement in the middle: if(isset($_POST['my_plugin_hidden_field_form_submitted'])) is incorrect. The challenge is asking you to check to see if "my_plugin_hidden_field" was submitted. Change your code so it checks that and that part of the challenge should pass. Also, make sure you are reading all 3 of the instructions carefully as the other part of your code does not match to what is requested in part 2 of the challenge.

Cheers!

Thank you Shawn but I don't get it. I have changed many times the code and revise several times the video but still no find the answer. Do you know any books or internet site where I can learn more about how to create plugins in WP?

Thanks

I am trying to finish the track of WP development and the course to create a WP plugin is the last one. I am far to create a plugin for WP. I am a student in the very beginning steps of WP and in this case I was asking for a book in order to support this stage. Thanks for your help if you can help me in this I will owe you a lot.

Saludos.

OK Shawn, thanks for help me. Let's talk about the challenge 1. I'll try to explain you what I did and why I left as I had it. I change the conditional field taking away -_form_submitted', and check only the field, 'my_plugin_hidden_field'. I did't look for other field because this is the challenge was asking me, to verify 'my_plugin_hidden_field. What other field I need to looking and verify? The reason why I left _form_submitted', is if I want to verify the field in question and the information submitted in that field is passed through into a form, I need to test if that form was submitted. It's my understanding,after review several times the video, that if I check the input field from (my_plugin_hidden_field) I'm also checking the form and field. But obviously all of that is wrong. Now where is the part of the field that is wrong?

global $plugin_url;

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

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

}

Hey Shawn, It passed. The three challenges. Thanks for your time and big help. As you said, the challenge was asking specific info. Once the first challenge passed the rest was easy. Once again THANKS.

Go USA!!! (no matter if we were eliminated from Brazil 2014)

<?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_username'] ) ) { $my_plugin_hidden_field_username = esc_html( $_POST['wptreehouse_form_submitted'] ); }

}

?>

Why isn't this working

<?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'] ); }

require( 'includes/page-wrapper.php' ); ?>

pliz help guys

<?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'] );

    }

    require('includes/page-wrapper.php');
}

?>

Ed,

Are you asking how to complete the code challenge or how to write a plugin for WordPress? If it's the code challenge just reread my post above carefully. if it's the later, there are loads of places online that'll teach you how to create a basic plugin. Looking at WordPress's documentation on creating plugins can be a good start. You can also watch the entire series here on how to create the plugin Zac is creating. You don't have to do the quizzes or code challenges to watch the entire series, just go to the list of episodes and click on the videos. I thought my explanation was good enough to assist you but if it wasn't I can help you further understand how to successfully complete the code challenges.

Cheers!

Ed,

I'm very happy that you got the challenges to pass. Some things to keep in mind for the future:

  • Read the instructions carefully and look for special words or variables that the instructions are asking you to use in the challenge as most challenges are looking for specific pieces of information and usually won't pass even if it's syntactically correct.
  • Take your time and make sure you review the code you typed before you submit to make sure it is satisfying all the instructions for that challenge.
  • Don't over-complicate things. Most of the time it's so simple but we second-guess because we usually don't want to believe the most simplest of answers is the correct answer.
  • Remember that all challenges are strung together. Challenge 3 has to have the code of challenge 2 which has to have the code of challenge 1 and all previous challenges must still be able to pass.

Please take you time and have fun doing these videos and challenges and remember that if you have issues in the future put them here and a teacher or student will be more than happy to assist you. Have fun and I wish you luck in your future endeavors.

Cheers!

Zoe Peng
Zoe Peng
10,809 Points

You should write these code:

<?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'])){
  }

}

?>
naiara gonzalez
naiara gonzalez
16,778 Points

<?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'] ); }

require( 'includes/page-wrapper.php' ); }

?>

Zoe Peng
Zoe Peng
10,809 Points

and 3rd challenge is:

require( 'includes/page-wrapper.php' );