We’re now in part four of this lesson of Using Custom Post Meta in the Thesis theme. We have succeeded in installing and setting up this MetaBox Class. The next thing for us to do then is to actually use it to create our meta boxes. We’re going to begin by talking about using the WPAlchemy Class.
Three-Part Process of Using the WPAlchemy Class
Using the class is actually a 3-part process. The 3 parts of this process are first, defining the meta box, secondly, defining the meta box form and finally, using the meta box data or getting the post meta data. We’re going to start that whole thing off by defining the meta box. If we come back to his site, here’s what the definition of a meta box looks like. Let’s just go ahead and copy his meta box definition and paste it here.
Meta Box Definition
The meta box definition includes first, an extension of the WPAlchemy MetaBox Class. What we’ve done here, we’ve said $custom_metabox = new WPAlchemy_MetaBox and once you’ve extended the WPAlchemy MetaBox Class, these are the options that you can set. There are a bunch of options that you can set and the options that we’re going to talk about here in this course now. Some of these you have to set, for example the ID. You need to set an ID for your meta box and this needs to a unique ID.
You set up a title and that is the name of the meta box when it shows up on the meta box. You setup the template and in this case, the template is the template file that you’re using to define the form. You can define the types of posts that this post meta applies to. If you leave it blank, it’s going to apply to posts and pages but you can specify either a post or a page or a custom post type or posts, pages and custom post types. This is where you would put the list of post types that this meta is going to apply to.
Context and Priority
Next you have context and priority. Context is the location of the page where the meta box is going to show and you have an option of normal, advanced and side. Normal is right here, this is the side and advanced just moves it all the way up here at the top then you have priority, high and low. The priority says, are you going to push this up above other boxes or you can just going to let it stack below other boxes. That priority is something that really only takes a fact until somebody starts moving boxes around and the priority will be ignored if somebody moves the meta box from one place to another.
Using the Function Mode Extract
The next part is the mode. This mode is a little complicated in its concept but what his MetaBox class does by default is it creates a serialized array of all of the meta information associated with that meta box. I’m actually not sure why he did that but for whatever reason that he did that, he gave an option of specifying either a mode of either array or a mode of extract.
Now, the MODE_ARRAY happens by default but we’re going to consistently use is the MODE_EXTRACT. The reason why we’re going to use MODE_EXTRACT is because we’re going to use that traditional WordPress get_post_meta command to access our post meta. If you want to use that system, then you need to use WPALCHEMY_MODE_EXTRACT rather than WPALCHEMY_MODE_ARRAY so we’re going to explicitly set the mode to extract.
Adding Custom Prefix
There are a couple of other parts to this. You can include or exclude templates so when we create meta boxes to select custom templates, we’ll be using that system to include the custom template then there are some filters which go beyond the sort of the scope of this conversation. The prefix allows you to add a custom prefix to each piece of post meta. It’s absolutely essential that you do this but you can either do it automatically by using this prefix setting or you can do it yourself by manually adding a prefix in your meta.
Now, I think for beginners it’s easier not to use this prefix system but just to go ahead and enter your prefix manually. That’s what we’re going to do but if you feel like you want to use this prefix and skip over that manual thing then you can do that too but I always use the manual system. Those are your setup options and we’re going to define a bunch of those options when we create this.
Create an HTML Form
The next thing to do then is to create the form. Creating a form in this case is creating an HTML form. In fact, let’s just open up an example of his HTML form, let’s open up the simple meta form. This is an example of that simple meta form.
The first thing he does is he wraps it in a wrapping div with a class of my_meta_control which gives him the ability to use this meta.css so now it’s in his meta.css file, it’s got my_meta_control and specific CSS associated with that. You can add your own customized CSS to this as well and that works because it’s wrapped in this class, my_meta_control.
He has some standard HTML for maybe doing a description and a title then really the guts of the meta box which is defining the field. Now, this section right here, $mb- >the_field is where the name of the custom post meta is assigned, is created or is defined. In that example where I created that ‘Rick name meta custom field’, that would be defined as ‘Rick Meta here’, that would be the name of this custom field. Then you have your inputs so you define the field and you define the input type.
In this case, once you’ve defined the field, then that field is active and you can specify the name which in this case is ‘title’ and the value which is the value associated with the custom post meta in this case ‘title’. This is what the custom post meta is called and this relate to each other, they’re the same thing. This is what the custom post meta is called and this is the value that is stored in that post meta name. I’ll show you exactly what that means as we work our way through this but those are the parts.