Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If you have been through the pre-WordPress 2.7 compatible option editing videos, or have ever done option editing using what is considered the old way--the deprecated way--you'll find that the new methodology introduced in 2.7 will make your life easier and your code cleaner. Basically, instead of doing all of these post-checks in creating nonces ourselves, we are essentially going to register our options with WordPress and let it handle all of the nonce nonsense. So let's take a look at our cc_comment plugin, and right now we're submitting using the WP nonce field in the check_admin_referer.
Well, instead we are going to do it using the new settings methodology. So in order for that to work, we need to register whatever settings are available for the plugin. So let's create a function. We will call it cccomm_init, because it's essentially just going to initialize our application. Then what we are going to do in here is we are going to use this special function called register_setting. It's going to take, essentially, the group of setting that we want to register under and then the specific setting we want to register.
So you may have multiple ones in a single group, and it allows you to essentially organize them how you want to. So we'll call this group cccomm_ options, and the specific setting we are registering is cccomm _cc_e-mail. Notice that's the same name as the option we are using before, and that's actually storing it in the same place. In order for this function to run, we need to register with a hook. So we are going to use the admin init hook, add_action, admin_init.
We are going to call cccomm_init. That will then run this function, which will register our options. Now there are some things we need to do to make our form page work correctly. The first thing, let's copy our settings name and make sure that anywhere where we are referring to that data, we use that information. So the ID is going to correspond to the settings name. That's important.
Then we can erase the processing, because WordPress is now going to handle the form submission for us, because we are going to submit it to the options page, at options.php. We can keep to get_option the same, because we are actually getting the same thing. Everything else will basically work. We no longer need to introduce nonces, but we do need to put a call to the settings_fields option, and this method is going to output nonces and everything for the entire group.
So notice this is the group that we are referring to. So you might have a number of fields that you're grouping together, and you only have to run this function once. So by using the Settings field, we can go back to our WordPress admin, we can change this to whatever we want to, and submit it just like we did before, and it will save the settings. If you look in the source, notice it's submitting the options PHP, and notice that it's generating this nonce information.
So that's automatically happening because we're running that settings field function. So as you can see, this is a much cleaner and easier way to do it. So if you're developing a plugin options page currently and don't need to worry about compatibility with WordPress 2.7 prior to, then it's better to use this API. The old method is deprecated, which means it's no longer supported and at some point they may remove it from the WordPress code. In addition, you can call, if you want to, a register setting function that allows you to specify a function that you can use to do any sort of sanitizing of the data before you submit it to the options.
So all in all, this is a much cleaner and better way for saving your options in the WordPress database.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 103228 Viewers
61 Video lessons · 89962 Viewers
71 Video lessons · 73537 Viewers
56 Video lessons · 105080 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.
Your file was successfully uploaded.