Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When we have created a plugin that requires configuration options, it's going to be necessary to store the options that the user enters. There is a couple of ways you can do that. One, if you have a lot of information that you are storing, you will probably want to create your own table, and we'll talk about interacting with the WordPress database in future units. The other way you can do it is you can create your own fields in the options database. We are going to look at how to do that. There are actually a couple of ways to do it. One way is the deprecated pre-WordPress 2.7 way, and I want to go through that, not because it's a great way, but because you'll see it a lot--it's a very common way of doing it--and also it allows us to get into nonces, and talk about what those are and how they work.
So I am just going to get you introduced and started on that way, but just keep in mind that this isn't a secure way, and this isn't a best practice. But it is a way that works if you want to do it this way. So let's look at our cc_comments plugin. We've already got it plugged in, and we've already got it set so we have an options page. So what we really need to do now is create a form. So we'll write form, action. We'll leave that empty, which means it's going to submit to the same page. That allows us to do the processing from within the same function. And the method is going to be post, id.
It's possible that there could be other forms on the page, so it's good to keep it unique. So there is our form. Now, we will create a header, and here we are going to say, call the field cc_e-mail. So we can link to it that way, and we'll go ahead and say, "Email to send CC to," and then stay in the h3, so it's on the same line, and we'll add an input tag.
So text input, id, cc_e-mail, the name would be the same, and then for the value we are going to output the result of calling the special function get_option, and we are going to give the option a name. In this case, we're going to call it cccomm_cc_e-mail. Notice I am using the prefix here, cccomm. I do that because it keeps it unique, so that way all these options are stored in the database.
So I want to make sure I don't override or get something from some other plugin. So I am also going to use the Escape function to essentially get rid of any HTML tags or something. Anytime there's a user-edited field, it's a good idea to do that. So that's my value field and the end of my input. Now, I just need to put in a submit tag, and we're good to go.
So let's go back to our form in the admin. Notice I didn't have anything here. So now we can see I have a form. Of course, I'm not saving it yet. In order to do that, I have to do a couple of things. Like I said, this isn't the best way-- I forgot to close that P. This is the old way to do it. You put a hidden form, and we'll call it cccomm_hidden, and the value will be Y. Then what we can do is when it's submitted, we can look to see if it exists in the post--single quote there, cccom_ hidden and equals Y. If it does, then we'll do our processing.
The processing is easy. There are two functions you can call: add option and update option. Update option is a good case for what we are doing here because if it doesn't exist, it will automatically create it. Add option is something you might want to use if you're setting something up maybe on activation. So we will just put update_option. The name of the option is going to match what we are using here in the get, so you can just copy that, and the value is going to be what's submitted for the post in cc_e-mail. That's it. So it's saved.
So now, I am just going to output some simple message that the user knows that it worked, and this updated class is part of the style sheet of the admin. Let's see what it looks like. So now, I'll save it, and I can go back here. Now, I want to make sure that I refresh it, because I want to make sure that I get that hidden field. So you can see here is where my function is outputting, and you can see I have my hidden field.
Then we enter database and save it, and you can see it tells me it's saved, and the get option gets it, and now puts it here. So for plugins that require editing, we have the ability to edit directly to options database in WordPress. It's pretty straightforward. In the old way, we create a form, and we also create the form processing, and use update option to update that option. We can use get option to retrieve the option whenever we want. The way that we did it here is insecure because anyone could spoof that they're coming to this and submitting data, and that could cause a problem.
We'll talk about how to avoid that in the next video when we use nonces.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104548 Viewers
56 Video lessons · 116470 Viewers
71 Video lessons · 85677 Viewers
131 Video lessons · 41006 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.