What is WPAlchemy?
That’s what we’re going to be doing then, is looking at the WPAlchemy. What is WPAlchemy? Let’s just seach for it, wpalchemy and you can see this WPAlchemy MetaBox PHP Class. By class, we don’t mean classroom type of class obviously. What we mean is class method in PHP but this is created by a guy named Dimas Begunoff who has been an active member on the Thesis theme forum, he’s a Thesis theme developer.
He created the system for simplifying the creation of meta boxes and he’s got code that you can download and he’s got descriptions on how to use this code. If you’re a programmer, he’s descriptions are really easy to follow but if you’re not a programmer or if you’re learning programming, I think it can be a bit more difficult to hang in there with him.
He also has a nice video here on how to use it but we’re going to ignore it. The reason why we’re going to ignore his videos is because he’s actually designed a system that can be used with any theme so he has a method that he describes for using his system with any theme and we’re not going to use that system. We’re going to use his class but we’re using the Thesis theme so we’re going to use the Thesis theme system of customization for implementing the WPAlchemy Class.
Thesis Theme System of Implementing the WPAlchemy Class
If you watch his video, you will see that there is a difference between what he teaches and what I’m teaching even though it’s his class. The reason I’m teaching it this way is because this makes updating and upgrading it as the Thesis theme upgrades much simpler. That’s what we’re going to be doing, is following my system instead of his but we’ll still use his class.
Now, the place you’re going to get his code is at his farinspace account at github and you can scroll down here to the bottom where there’s a link to that, here we go. You can go to this github link and also have a link on that page so you can just click on that link to get this material. But this takes you to his github place and it automatically takes you to the dev branch. I’m not going to describe how github works so you’re just going to have to trust me on this.
The first thing you want to do is switch from the dev branch to the master branch. Once you’ve switched to the master branch, you want to download the zip file. This zip file contains some files, Credits, License, ReadMe and it also contains a folder called wp-content. Inside that folder is a folder called themes and a folder called wpalchemy. Inside of themes is a folder called mytheme and metaboxes and we’re going to rearrange the way he has these things set up since we have it downloaded.
Creating the Folder
The first thing to do then once you’ve downloaded it is to extract it. Here’s our file and I’m going to extract to where I want to use it which in this case is Websites, byobwebsite, tutorial sites and Lesson 17. Then I’m going to open wp-content and copy this wpalchemy folder, go back to my Lesson 17, wp-content, themes, thesis_185 and my custom folder and I’m going to paste wpalchemy there. Then I’m going to back to Lesson 17, farinspace, wp-content and open his themes and mytheme, take his metaboxes, copy that, go back to my themes, thesis_185, custom folder and paste it here.
Now I have his wpalchemy folder and his metaboxes folder inside of my thesis_185 custom folder, that’s where we’re going to start. Before we transfer these to the site, we’re going to open up metaboxes and create a new folder inside of that called example-files and to take all of his example files except for setup.php and meta.css and move them into example-files.
Now the only thing that’s directly inside of metaboxes is meta.css and setup.php and what I’m going to do is delete setup.php. I’m not going to use setup.php and I don’t want it to be confusing to anybody so we’re just going to delete that.
Inside of meta boxes, we have his set of example files and now let’s just talk for a moment about the way these files work. He’s got this wpalchemy folder which has two files that you will not change and these are his MetaBox Class so we’re just going to leave these two files inside this directory and leave them alone.
Then we have our metaboxes directory and what that is for is for us to create our templates for our meta boxes. He has given us his example files and each files essentially has two types, there’s the meta file and there’s the spec file.
The meta file is essentially an example form for in this case, a checkbox and the specification file, the spec file is an example of the meta box definition. I’ll show you what that means here in a few minutes but that’s what’s going on here. We’re going to be relying on these examples to create what we’re really using so we’ll be relying both on the checkbox-meta and checkbox-spec, simple-meta and simple-spec.
Create Actual Meta Box Template Files
The four range of the things you can do with his class can be found here in one of these files. That’s the structure we’ll create our actual meta box template files, inside of this directory called metaboxes. Now that we have that set up, I’m going to upload those to my site so we’ll go to 17 for a second here, wp-content, themes, thesis_185, custom and I’m just going to take metaboxes and wpalchemy and upload those to my site.
Let’s just pretend there’s going to be a Thesis 1.8.6, when you upgrade to that, you would simply take your custom folder with you, copy and paste it in the 1.8.6 which would bring all of these meta boxes with it. That’s the reason why we restructure this, is because the way you upgrade your Thesis theme installation is you take all of your custom folder from one Thesis install to the upgrade install so this way, all these just keep on following.
Setup Includes for the WPAlchemy MetaBox Class
We’ve got that setup, now the only thing we have to do is setup some includes. We’re going to set those includes up in our custom_functions file and I’m going to paste these includes in here. Actually, I’m not going to do that. I’m going to come over here and go to his example for how to use these. Come up all the way to the top with this and what we’ll do is take line 1 to 13, copy it, come over here to our custom_functions.php file and paste it.
The first thing we do is we include_once and we are including the meta box file and unfortunately, this is not quite accurate because if we come over here, right now we’re in our custom_functions.php so what we’re looking for is a directory called wpalchemy and inside that directory is a folder called MetaBox.php. What we should be saying here is include_once ‘wpalchemy/MetaBox.php’; because that’s really what the directory looks like.
Now, because we are in our custom_functions.php file, we don’t need to show the entire path, we only need to show the path to this file from where we are so we are including from our current location folder wpalchemy and MetaBox.php. Then what we’re going to do is unqueue the styles. The styles that we’re unqueuing, he’s created a set of styles, remember the meta box styles that we looked at where that we saw here under metaboxes, meta.css, this is the file that we’re going to unqueue.
It’s a stylesheet and we’re going to unqueue when we are in an admin situation so we’ve said, add_action( ‘admin_init’, ‘my_metabox_styles’ ) then if ( is_admin() ) wp_enqueue_style, he’s giving it a handle of ‘wpalchemy-metabox’ and he says, get_stylesheet_directory_uri which in this case is Thesis 1.8.5 so what we have to do is keep going with this and say, ‘/custom/metaboxes/meta.css’).
So get_stylesheet_directory_uri gives you the folder of the current child theme in this case it’s Thesis 1.8.5 so it’s going to give us the directory such and such Thesis 1.8.5 then we just need to go into that directory and say custom/metaboxes/meta.css. That successfully included the WPAlchemy MetaBox Class and all of its essential files so we upload that.