One of the major features to be introduced in WordPress 3 was the ability to create custom post types. Basically, this lets you recreate the ‘Posts’ tab with specific functionality in mind. For example, creating a portfolio with fields for project images and client information. At Shockoe, we are using this in an upcoming project to that uses WordPress to provide content for a new iPhone app.

This makes WordPress much more viable as a CMS as opposed to blogging software that struggles to handle larger sites. In previous versions of WordPress this involved either a considerable amount of work or using a massive plugin. The best of those plugins, Magic Fields (a fork of Flutter), is several thousand lines of code. We can create a flexible solution in far fewer lines.

Here we have created a new post type that will have it’s own sidebar tab under ‘Posts’ as well as fields for title, content (‘editor’), an excerpt, and comments. We also added functionality similar to WordPress’ tags and categories features by calling ‘register_taxonomy.’ Setting ‘hierarchal’ => true will make the field behave like categories while leaving it empty (or setting it to ‘false’) gives us the tags functionality.

That’s it. Simple. In a handful of lines we have a flexible solution that gives us the option of using better contextual grammar. That translates to a much better experience for clients and end users.

Adding Custom Functionality

The code above basically replicates the Posts functionality with the ability to have better labels. But what if we need to include custom information? For that we need add_meta_box:

Now we should have a custom text box that will appear below the content editor. When we save our post, the information in our custom field will be saved to the database. And when we edit our post, the text field will be repopulated.

Conclusion

With a little bit of coding knowledge you can create an elegant solution for turning WordPress into a functional CMS. And this is a very basic example; the possibilities of what can be created from this point are endless.