OOPost Types: Classes Part 1 – Object Oriented WordPress 3.0 App
An OOP Post Series On WordPress 3.0 Custom Post Types
I noticed how extremely in demand posts about custom post types are when I realized my previous post is apparently the most popular page of this site almost doubling the homepage in page views.
So if it's post types you people want to know about or just what WordPress 3.0 is now really capable of, then I will share with how to create classes for your post types, custom taxonomies, other various yet useful methods you can create for your classes, and then show you how to create objects which you can use in your WordPress theme to create a completely custom section of your site. I'll be incorporating some jQuery and Ajax even if all of that wasn't already enough.
I'll share with you some of the code that I've been writing and re-writing you don't even want to know how many times over again, in an effort to create what I want to call a super class for post types, but more on that later.
What you will learn
- How to create classes for custom post types
- Creating methods within your post type classes
- Using new objects of your classes in theme templates
- Plus much more to come...
I have been developing a complete application as a theme using multiple post types and taxonomies, classes and objects, and jQuery custom post forms which submit using the built in Ajax functions used to submit new posts in the dashboard, plus tons of other cool custom made capabilities.
It's a rather BIG project of mine that I started when 3.0 was still on the first beta version. Not sure how long ago that was, but I've been coding/designing this theme/app for MONTHS now, and it grows more complex and abstract every time I work on it. But enough about that, I could go on for days.
The OOPost Types Class Is Now In Session!!!
Since I don't really have a set number of posts planned for this series I'm not sure how to break it up. So I'll just start by showing how to build a class for a new custom post type.
You'll need to create a new file that will contain the code for the post type class and just name posttypes.php. In your theme directory create a new folder called /functions and save the posttypes.php file in there. This becomes useful in organizing your Php files and any custom code.
1. Prepare your functions file
Open your functions.php template for your theme and add the following code to the top before everything else.
// Create a variable to the path to functions directory $functionsdir = TEMPLATEPATH . '/functions'; // Include your posttypes.php file require_once ( $functionsdir . '/posttypes.php' );
The functions directory variable will be useful, and while were here you might as well include your posttypes.php file so WordPress knows to do something with it.
2. Declaring A Post Type Class
Now you can start creating the class in posttypes.php. In Php you declare a class with the word class followed by the name of the class followed by curly brackets { }. It's similar to the way you create functions in php only you can't use parenthesis like functions do for passing variables. It's common for class names to start with a Capital letter unlike functions too.
<?php
// Create a post type class for site posts
// To use as a bookmarking post type for sites you want to save/share.
class TypeSites {
// Do stuff...
} // end of TypeSites{} class
?>
Within the brackets you can create methods which are really just functions, but are called methods. I may use both to refer to them just so you know what I mean. They are the same thing.
So now let's drop the code in here that you may have seen many times before for creating custom post types. Here's how I have set up the labels and other vars for a post type called Sites using the $labels like you are supposed to now.
<?php
// Create a post type class for site posts
// To use as a bookmarking post type for sites you want to save/share.
class TypeSites {
public function TypeSites() {
$siteArgs = array(
'labels' => array(
'name' => __( 'Sites', 'post type general name' ),
'singular_name' => __( 'Site', 'post type singular name' ),
'add_new' => __( 'Add New', 'site' ),
'add_new_item' => __( 'Add New Site' ),
'edit_item' => __( 'Edit Site' ),
'new_item' => __( 'New Site' ),
'view_item' => __( 'View Site' ),
'search_items' => __( 'Search Sites' ),
'not_found' => __( 'No sites found in search' ),
'not_found_in_trash' => __( 'No sites found in Trash' ),
),
'public' => true, 'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'site'),
'query_var' => 'site',
'supports' => array('title','editor','author','comments')
);
register_post_type( 'site', $siteArgs );
}
} // end of TypeSites{} class
?>
I won't explain what all of these are for since there's 2000 articles on the web about it, but you can see that I set the rewrite to use 'site' for the slug, and the query variable is set to 'site', and finally what the post type supports is 'title', 'editor', 'author', and 'comments'.
We can use the title and TinyMCE editor that posts use instead of having to create meta boxes for, and we want to allow for showing the author of a site post as well as let people post comments to the posts. In another custom post type later in the series I'll show you how to utilize the TinyMCE editor as a custom meta box, not the way the default one is used.
The only thing that we need now is an input field for the site url. For that we create a custom meta box which we'll create in part 2 of Classes.
Instead of adding a custom taxonomy for this post type so that we can organize the sites submitted properly, let's just use the built-in Tags and Categories in WordPress. This seems to be the best way to organize different sites I've found, unless you know a better way then please share.
The way you incorporate the tags and categories taxonomies is by adding the following to the $siteargs array we've created.
'taxonomies' => array('category', 'post_tag'),
So now our class should look like this.
<?php
// Create a post type class for site posts
// To use as a bookmarking post type for sites you want to save/share.
class TypeSites {
public function TypeSites() {
$siteArgs = array(
'labels' => array(
'name' => __( 'Sites', 'post type general name' ),
'singular_name' => __( 'Site', 'post type singular name' ),
'add_new' => __( 'Add New', 'site' ),
'add_new_item' => __( 'Add New Site' ),
'edit_item' => __( 'Edit Site' ),
'new_item' => __( 'New Site' ),
'view_item' => __( 'View Site' ),
'search_items' => __( 'Search Sites' ),
'not_found' => __( 'No sites found in search' ),
'not_found_in_trash' => __( 'No sites found in Trash' ),
),
'public' => true, 'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'site'), // Permalinks. Fixes a 404 bug
'query_var' => 'site',
'taxonomies' => array('category', 'post_tag'), // Add tags and categories taxonomies
'supports' => array('title','editor','author','comments')
);
register_post_type( 'site', $siteArgs );
}
} // end of TypeSites{} class
?>
What's this public stuff?
We will be creating some public variables and functions in this class. What that means basically is they are accessible from anywhere in your script. It is the easiest to use access control modifier because of this.
Quick Overview of Access Control Modifiers
- Used for Variables or Functions...
- Public - Can be used from anywhere in the script.
- Private - Can only be used by the object it is part of, it cannot be accessed elsewhere.
- Protected - Can only be used by the object it is part of, or descendants of that class.
- Final - Cannot be overridden in inherited classes.
- Used for Functions or Classes...
- Abstract - Cannot be used directly. Must inherited from the functions or class first.
We need to create a public variable that is used to store an array of the post data 'title', 'content', 'siteurl', 'category', and 'post_tags'. Add the variable to the top of the class just after the first curly bracket like this.
class TypeSites {
public $meta_fields = array( 'title', 'description', 'siteurl', 'category', 'post_tags' );
The class should now look like this.
<?php
// Create a post type class for site posts
// To use as a bookmarking post type for sites you want to save/share.
class TypeSites {
public $meta_fields = array( 'title', 'description', 'siteurl', 'category', 'post_tags' );
public function TypeSites() {
$siteArgs = array(
'labels' => array(
'name' => __( 'Sites', 'post type general name' ),
'singular_name' => __( 'Site', 'post type singular name' ),
'add_new' => __( 'Add New', 'site' ),
'add_new_item' => __( 'Add New Site' ),
'edit_item' => __( 'Edit Site' ),
'new_item' => __( 'New Site' ),
'view_item' => __( 'View Site' ),
'search_items' => __( 'Search Sites' ),
'not_found' => __( 'No sites found in search' ),
'not_found_in_trash' => __( 'No sites found in Trash' ),
),
'public' => true, 'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'hierarchical' => false, // like 'post' not 'page'
'rewrite' => array('slug' => 'site'), // Permalinks. Fixes a 404 bug
'query_var' => 'site',
'taxonomies' => array('category', 'post_tag'), // Add tags and categories taxonomies
'supports' => array( 'title', 'editor', 'author', 'comments' )
);
register_post_type( 'site', $siteArgs );
}
} // end of TypeSites{} class
?>
Notice how the first function that has the post type registration code and stuff has the same name as the class itself. This is one way of creating the class __construct() method for the class, making it run automatically when the class is instantiated. I'll go over __construct() and __destruct() methods later on in more detail.
Building The Class Methods
In the next part of this series we will add some methods to this class, and really start to boogie down with some Object Oriented Php custom post types.







User Comments
( ADD YOURS )Christine Yorty August 11
I am looking to take a class in WordPress. I paid sometone to do most of the design work on my site, but I want to learn how to work within WordPress. Do you offer classes, lessons, or now of any in the Boston area. I am in BackBay
Jared August 12
Boston WordPress meetup group is having a class you might be interested in.
http://bwpmshop1.eventbrite.com/
Abdullah Al Mamun September 3
Great approach.
I'll definitely follow the total series.
Thanks a lot.
david bill January 18
Great approach
pass4sure 70-649
Don Gilbert March 21
This is great! But since WP is requiring PHP5+ in 3.1 and beyond, don't you think you should update the code to use PHP5 __construct() method instead? I know that PHP4 style construct still works in 5, but it's slower, since PHP 5 first looks for __construct(), and if it's not available will then look for ClassName method.
Don Gilbert recently posted..jQuery Plugin- odoTicker
Will July 26
Another good option is Wordcamp. It is organized in many cities around the country and is basically a 3 day conference for all things WordPress. Great for beginners, advanced, as well as business owners. I highly recommend checking it out and see if there is one coming up in your area. It's cheap too!
Will recently posted..Albuquerque Scooter Store & Repair Shop
Trackbacks