FREELessons: 6Length: 32 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 Creating a Meta Box: The Save Function

In this lesson, you'll write the function to save data from your meta box.

Related Links

2.2 Creating a Meta Box: The Save Function

Hello, and welcome back to this Tutsplus course on creating metaboxes in WordPress. In this part of the course, we're going to continue working on our code to create a metabox in the post editing screen. And what we're gonna do now is create a function that will save any data that we add to that metabox, because at the moment if we add any data to it, it won't do anything. So underneath our callback function, let's add a new function. Now this has one parameter which is the post_id. Before we go on to write the content of our function, we need to hook it to another action hook. And I'll make sure I type this one correctly this time, and the action that we use this time is safe post. So our first parameter is the action hook save_post, and the second is the name of our function. So now let's populate it. The first thing we need to do is check for the nonce. So firstly, we checking if a nonce hasn't been set, because if that's the case, we won't go any further. But it's not just if a nonce hasn't been set, it's also if the nonce can't be verified. So, we need o. So if either a nonce hasn't been set, let's have some spacing, or the nonce can't be verified. And this is for tutsplus nonce, which is the name of our nonce, not the action, which is tutsplus_metabox_nonce. So if neither of these is the case, We then return, so we do nothing, okay? So that's the first thing, so we check for our nonce, but if those aren't the case, we then continue. So now let's check if the current user can edit posts. So we're checking if the current user can't edit this current post, and again, if that's the case, we just return, so nothing will happen. So having run those two checks, we're now looking to do something if neither of those things are the case, so neither the nonce isn't correct or the current user can't edit posts. So the first thing we need to do again, is a check, so we're checking if our custom field has been set for this post. So we're checking if tutsplus-metadata-weather has been set for this post, and tutsplus-metadata-weather, this is name that we gave to the field. So what we're checking here is whether any data has been added to this field. We're not checking whether there's a value against this particular custom field, this piece of post metadata which is tutsplus_weather. Because we only need to run this if the user has added something in that field. So first let's create a new value. So our new value is what's being typed into tutsplus-metadata-weather for this post. And then having to find that new value, we save it to the post metadata using the update post meta function. Now, that has three parameters, the first of those, let me just type in all my spots for those. She that doesn't have quotes. So the first of those is the ID of the post, so that's post_id. The second is the value that we're updating, and that is this tutsplus_weather ID, which is the post metadata. So you see that we use tutsplus-metadata-weather first time to identify what had been input into our field, and then we save that value to the tutsplus_weather metadata. And then the third parameter is the value that we're updating that post meta with, so that's new value. So that's our save function. We check for the nonce, and I'm just looking at that nonce and realizing that this wp_verify_nonce function needs another parameter. So, after this closing square bracket, we need tutsplus, _metabox_nonce. Because the second parameter, the first one is the nonce and the second one is the action. So it's important to include both of those parameters but the first one, the set, is set function, just uses the nonce itself. So let's save that and then I'll go to my post on my site. It showed sunny because I tried doing that earlier on. I'm gonna put overcast, because the sun has gone in now, I'll hit Return to save that. My post has been updated and you can see that today's weather is overcast. And in this case, I've turned off custom fields on my screen options, but if I show you that, you can see how this has been stored as a custom field. So here on my custom fields, and I've got tutsplus_weather, the value is overcast. And I'm gonna turn that off again, because I think having a custom metabox like this is a much neater and more user friendly way of letting your users work with post metadata. So that's how you create a custom metabox to display on your post editing field to capture post metadata. Now obviously once you've done that, you can then display that on your site. But in the next part of the course, I'm gonna show you how to create a metabox that will appear over here on the right hand side. And what it'll do is give you some guidance on how to edit this particular post. See you next time, and thanks for watching.

Back to the top