Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
As plugin developers we rely on forms to get information from the user. In this video we will look at creating forms and more importantly submitting them to use in our plugins. Once we know how to accept data from the settings page we can then move on to storing that information in the database for easy access.
-
0:00
[Zac Gordon] At this point in our PHP development career, we should know how to work with forms.
-
0:06
Still, let's take a moment and do a quick overview of how forms work
-
0:12
especially within the WordPress environment.
-
0:15
The first thing to know is that in WordPress, we used the post format,
-
0:20
which means that the data is pass behind the scenes directly to the server and not stored in the URL like a get request.
-
0:28
We will have a form in our plugin setting page for the user to enter their treehouse username.
-
0:34
When we design the form, we will set up the opening form tag
-
0:38
with the name attribute, a method attribute set to post, and then an empty action field.
-
0:45
The empty action field ensures that when we submit the form the content will go to our main plugin file.
-
0:53
Even though, we're calling it from an included plugin settings template file.
-
0:59
Our form fields will include a hidden field with the name attribute of wptreehouse_Badges_form_submitted
-
1:07
with a simple value of Y for yes.
-
1:11
This will let us test that the user has submitted the form.
-
1:15
We will also have a field for the user to enter their username.
-
1:20
Once the form gets submitted, we will check in our main option page function that the hidden form is set in a value equals Y.
-
1:30
As with all form data, we will make sure that we properly sanitize
-
1:34
and escape the data to prevent the user from entering malicious code.
-
1:40
We can then test the user has filled in the username and escape that content as well.
-
1:46
With the username stored, we could check to see if a profile page exist on the treehouse site,
-
1:53
which we'll look at how to do in the future video.
-
1:56
If either the username has not been submitted or profile page is not exist for that user, we will then reload the form
-
2:04
and display a relevant message.
-
2:06
If you've worked with PHP forms before, this should all make sense.
-
2:11
Now, let's jump in, code our form, and runs some simple validation.
-
2:17
The first thing we're going to do is come into our option page rapper file down to our form element
-
2:25
and create a name attribute
-
2:27
that's equal to wptreehouse_username with form at the end.
-
2:35
I've also noticed that we have a spelling mistake where we forget the r in username.
-
2:42
This would cause us big problems later on, so let's go ahead and add it now.
-
2:52
Now that we have that setup, we could come in and create our hidden field that tells us whether the form has been submitted.
-
3:02
We'll create an input field
-
3:05
set the type equal to hidden
-
3:10
the name attribute
-
3:13
equal to wptreehouse_Form_submitted
-
3:20
and then set the value equal to Y.
-
3:26
Now that we have this done, let's come back into our main plugin folder, come down into our options page,
-
3:33
and write below where we'll pull in the plugin URL, let's create an "if statement" to check that our form has been submitted.
-
3:43
To do that, we'll write an if statement and then say if a set
-
3:54
the post value wptreehouse_Form_submitted then do the following.
-
4:04
If we know that this form has been submitted, we could then come down and assign a variable called hidden_field
-
4:15
set it equal to this post value up above, which we'll copy
-
4:22
and then for security purposes, we're going to wrap that value inside of an escape HTML function.
-
4:33
Next, we're going to check to make sure that our hidden_field value is set to Y.
-
4:47
If it is then we'll create a new variable called wptreehouse_username
-
4:55
and assign that to value of the escape
-
5:03
post value wptreehouse_username.
-
5:09
Now, that we have this set up, if the field has been submitted, we should get our username.
-
5:16
Let's go ahead and write a brief echo statement here
-
5:23
come back into our plugin page, try submitting a username,
-
5:30
and we should see it displayed at the top of the page.
-
5:34
However, if we come back to this page again, that value no longer appears.
-
5:40
This is because it isn't being stored.
-
5:43
So, a little trick to get around this is that if we enter in a value, hit submit.
-
5:50
We see it and if we refreshed the page, it'll ask us if we want to resubmit that form data again.
-
5:59
If we hit continue, then it will still have that value.
-
6:04
Now that we have that value, we're going to write some simple conditional statements
-
6:09
then make sure that if the username is blank or not filled in, it only shows this first box.
-
6:15
However, if the username has been submitted, it will show the recent badges as well as the profile information.
-
6:23
Let's come back into our main plugin file, delete our echo statement,
-
6:30
and then come into our options page rapper include.
-
6:34
We'll start right at the beginning of the first post class for the let's get started message.
-
6:42
And, we'll write a conditional statement that first checks to see if the username has not been set
-
6:55
or the username has been set
-
7:00
but it's not equal to blank.
-
7:06
Then, we'll come down at the bottom of our post box
-
7:13
and write an outstatement
-
7:21
come down to the bottom of the next of post box
-
7:24
and then close our if statement.
-
7:31
Then, we'll come back to our plugin page
-
7:34
click on it to make sure the form data isn't save,
-
7:39
and we could see that we just have the field that we want.
-
7:43
However, if we entering a value, we should no longer see this field and instead see the most recent badges.
-
7:51
Let's click save and we could see that something isn't working here.
-
7:56
It probably has to do with our conditional statement
-
7:59
and if we come back up it looks like we're saying if the username is not equal to blank
-
8:05
but what we really want to say if the username is blank.
-
8:10
Let's go back, try this again,
-
8:16
submit it, and wee see that it's properly working.
-
8:19
Now, we know that we have a working conditional statement and we can return back to this profile section
-
8:25
and make sure that it only displays when the username is submitted and not equal to blank.
-
8:31
So, we'll come back into our rapper page, copy this if statement here,
-
8:39
come down to the post box for a profile information, paste it in
-
8:46
and then make one small change.
-
8:48
We actually want to make sure that if the value is set and is not equal to blank
-
8:57
then display the profile information.
-
9:00
We'll come down under the post box
-
9:04
and close everything off.
-
9:07
When we come back into our page now,
-
9:10
we could see that when we start, all we have is the request for their username
-
9:18
and then when we submit it, we see the information show up.
You need to sign up for Treehouse in order to download course files.
Sign up