7 days of unlimited WordPress themes, plugins & graphics - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 13Length: 1.7 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.2 Initializing Options and Creating the Admin Menu

Let’s start by initializing our options. We do that because our page depends on querying the database for a record, so we need to make sure that record exists, even if it’s the first time we’re opening the theme options page.

3.2 Initializing Options and Creating the Admin Menu

Let's start by initializing our option and this is something we must always do, because getting options from the database requires us to query that database for us specific record. And we need to make sure that we find that record there even if it's the first time we're using the theme options page. Now, we didn't do that last time, because it was a very simple page. But now we're just starting to add more and more layers of functionality. So right here, I have my phpMyAdmin, and all the way down is the ap_options. What I'm gonna do is delete it from here. So that's deleted the option from the database. And right now, we're gonna go back to our code, we're gonna go in the other functions. I went ahead and created these comments here. We're gonna use the other function section to create a function called ap_init_option. Let's add a comment here. I find that adding the comments right before writing the function, really saves me a bunch of time later. So what you need to do here is say, option or options, get option and the name of our setting, which is ap options. Now get option is a function that will return the value of ap options, if it's finds it. Otherwise it's gonna return false. And we can use that we can say if false triple equal options. So we're basically saying if we didn't find ap options in the database, we'll simply say add option ap options. Now by itself, this function will do absolutely nothing, so we need to tie it to a hook. And we'll go into here to hooks, and we're going to add it, we'll say add action to after setup theme. It's gonna be called ap init option. The other arguments are not necessary. All right, so we're gonna save this and now if we go back and we're gonna refresh. If we hit browse again. You'll see that all the way at the bottom, we have a new entry called ap options. So our script searched the database, saw that there was no ap options entry, and it created one for us. All right, so now let's go ahead and create the admin menu. This is actually very similar to what we did previously. So I'm just gonna copy the code from the previous page. I'm gonna start with a hook right here, hooking into admin menu. And we're gonna use the function ap_update_menu. And the function looks like this, add_theme_page. We call it theme options, we give it the ID of ap theme options, and the callback function's called ap render theme options. Let's go ahead and write that function inside the render section right here. Okay, so what are we doing here? Well, we're doing a couple of things. First checking if the user can access this page otherwise we exit. Then we're creating the main wrap container doing an h1. And then this is a new addition. We're doing settings errors. This is a function that's gonna allow WordPress to display any errors that we might get, or success messages when we are saving the values in the database. We have the same form as last time, we're doing settings field, setting sections, and submit button. The only difference is we're inserting a horizontal rule between the sections, or the content, and the submit button. So now, if we do a refresh, we have a page with the title and the save changes button. And the menu link is back in its place here under appearance. All right, we're making progress. Now let's see about registering the settings and displaying the fields using those two multidimensional arrays. That's coming up in the next lesson.

Back to the top