You Down With OOP? WordPress Class Based Coding: Part 2 – CPTs and Taxes

Hi again all, last week we took a look at starting to use Classes in your WordPress development.  As the uber smart John Havlik pointed out, that example didn’t really use any OOP (Object Oriented Programming) practices.  Rather it simply used the Class to namespace our functions.  Today we’ll look at registering a CPT (custom post type) and a Taxonomy together in a Class which will start to actually utilize some OOP.

We start by creating class-2014-post-type-modal.php in our /inc directory found in the root of our theme.  Here ‘2014’ should be the name of your theme and ‘modal’ should be the name of your CPT.  These tutorials will eventually build the example from our first lesson and create a CPT  (this tutorial) that uses the img tag and creates a Modal for that image with custom meta displayed (like a photo credit, date taken, location of image, etc…).

As before lets add the following to our functions.php file:

With our class-2014-post-type-modal.php we will start with:

This is similar to our first Class we wrote though with a few key differences.  First we have a ‘__constructor’ function.  This function tells the class what to fire when initiated.  Second you’ll notice the:

Which ends the file and is found OUTSIDE of the Class itself.  This actually initiates the class, thus telling it to fire off what ever is in the constructor.  Because we are calling functions that are inside of the class you will also notice that we are not declaring our ‘register_cpt’ function as ‘static.’

Let’s move on by actually calling our ‘register_cpt’ function from the constructor by adding:

Here we have a pretty standard ‘add_action’ with the only difference is the way we refer to the function itself, using ‘array( $this, ‘register_cpt’ )’ rather than just the function name.  This would be a good time to point out one of the reasons you should be using classes.  Normally defining a function as ‘register_cpt’ would be a very bad idea, as there would be a very high chance that some other function (from a plugin or in the theme) also has a function called ‘registered_cpt’ which would cause a fatal error.  Since we are inside of our Class_2014_Post_Type_Modal here though, this is ok, and actually preferred.  One of the benefits of Classes is that they allow us to have function specific to this context and have shorter and more semantic function names.

To test that everything is working lets echo out some text in the ‘register_cpt’ function.  Something  like this should work:

You should now see ‘I will work :) Your first function called by a constructor’ at the top of you site, above everything since we are firing this on ‘init.’ (you can also play around by changing ‘init’ to ‘admin_footer’ to see your echo-ed text in the admins footer rather than at the top, though change this back to ‘init’ before moving on).

Ok, time to actually get our CPT going.  A standard approach would be something like:

And this would work fine, but let’s clean it up a little.  Having been working at Alley Interactive for some time now and having many of my Pull Requests go through Matt Boynes (twitter) many habits have become part of my daily practices.  First of these habits is to try NOT to declare variables when you don’t need to.  Keeping this in mind we can change our ‘register_cpt’ function to:

This saves us from declaring a bunch of default arguments as well as declaring the $label and $args variables, Yey :)

Now that we have our CPT working we can add a Taxonomy for categorizing our modal images, we will be using the image’s location, or where it was taken, for this example.  Rather than register a new function for this, we can just throw the code below register_post_type code. One advantage of this (rather than registering the taxonomy in another file) is the ‘public $cpt = ‘modal_image’;’ line near the top of our Class.  This is used to define the CPT’s slug as well as which CPT our taxonomy will be applied to.  Should you ever need to change the CPT’s slug, we only need to change it here once and the taxonomy will come along with it. Now our ‘register_cpt’ function should look like:

And our entire files will look like:


With all this in place we can take a break for now.  Next week we will start getting our frontend functions in place as well as add some custom meta information.

Note: We will actually be applying meta data to media objects rather than the CPT we created, the logic for this will be explained in our next tutorial as well.

Leave a Reply

Your email address will not be published. Required fields are marked *