Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The WordPress options database has a number of options stored in it. Some of these are built into WordPress, and they might be handy to use in your own applications. In addition, you might want to be saving your own settings in here, and having them displayed in the built- in setting pages in the admin. So we're going to talk about how to integrate with these options. Let's go into the WordPress admin and take a look. There is a page you can link to-- you have to go there directly; there is not actually a link--in the admin menu called options.php, which contains all the settings in the database.
So you can see this is all here. Some of these are key, and you may want to access at different points in your application. You can always use get_option to refer to these just like we referred to our own options. So you can see the admin_e-mail is a good one. The blogdescription can be helpful. The blogname can be helpful. We also have information about the web site, what the URL is, stylesheet, all kinds of good information. In this page itself, you can actually edit this data directly.
So if you ever want to just quick change an option, you can do it right from here. If you want to integrate your own options into the built-in Settings pages, you can do that, if you remember our cc_ comment plugin that we created allows us to edit a cccomm_cc_e-mail. Let's move this into the settings page. So first we can delete our existing form page. We won't need any longer. Then we can delete what's in the existing function to handle adding it to the menu, because it won't need to be in the menu any longer.
The first thing we're going to need to do is create a function that will generate an input field to handle this setting. So we'll create this, and we'll call it cccomm_setting_field. In here we're simply going to create input type = "text" name = "cccomm_cc_e-mail" id = "cccomm_cc_e-mail," and the value we're going to use get_option.
So echo get_option cccomm_cc_e-mail, and then close a tag, and then we'll want to of course go back into php. So we don't need to do anything else with this. You just need to make sure that the ID and the name field match the option. Then to add this into the Settings page, we're going to use the special add_settings_field. You can specify an id, cccomm_cc_e-mail.
You're going to specify what you want to read for the label of that field. Then you want to specify the function that's going to generate the field. So this is going to be cccomm_setting_ field, and finally the section it's going to be, which will be the general section. Notice we're already adding this into the admin_menu hook, so that should put it into our menu for us.
So if we go back to our General Settings and we refresh it and we scroll down, we should see our CC Comments has now been added to this page. If you want to have multiple settings and create different sections, there is another function that enables you to create an entirely new section heading. So you'll create a function that outputs the section, and here you can just do something simple Settings.
Then we'll use this add_settings _section to add it directly in. So we say cccomm, which is an ID of the section. We have a name. We have the function that's going to generate. It's going to run once, essentially, at the beginning of this heading. So this will be cccomm_setting_section, what we just wrote, and where you want it to appear, in this case in the general section.
The only other thing you need to do at this point is specify if your fields are going to fall under that section. So there is another option here where you can specify that, and we just use the ID that we created for that section. Now if we go back to our Settings page, we can reload, and you'll see we have our CC Comments section. Notice it outputs the name and then whatever you had in your code, and then we would have whatever fields afterwards. So we can access any of these settings that are stored in the options database.
We also, since WordPress 2.7, have the ability to add our own items directly into the Settings pages.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97514 Viewers
61 Video lessons · 84752 Viewers
71 Video lessons · 68931 Viewers
56 Video lessons · 101381 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.