, ,

Abstracting Custom Post Type and Taxonomy Registration In WordPress

Custom Post Types in WordPress are incredibly useful, but as a web developer working on a number of projects at any given time, it can be frustrating having to manually write out each and every label needed for display throughout the dashboard.

While reviewing plugins and other projects, I’ve seen developers build libraries to abstract away a lot of the mundane details of creating CPTs by dynamically generating default arguments and labels. Unfortunately, there are a few pitfalls with this approach that don’t make it a viable option:

  • The libraries typically don’t actually account for all the necessary strings needed throughout the dashboard, meaning some of them fallback to the defaults.
  • The rules for generating the plural form of a word can be fairly complicated, so the dashboard will ultimately be littered with misspelled labels when the library does nothing more than tack an ‘s’ on the end of a word to create its plural form.
  • The biggest issue, however, is with text that needs to be translated. The tools used for translating strings don’t actually execute the code, but rather parse it as plain text, looking for any translatable patterns. So all those labels dynamically generated by the library are effectively invisible and can’t be translated. This is also partially why it’s a bad idea to use a variable or constant as the text domain in WordPress’ translation functions. A unique, hard-coded string should always be used instead.

If it was really as simple as some of these libraries try to make it, it would have been done in core.

Aside from those issues, I like to keep all of the arguments in a single location so they can be quickly referenced and make the code easier to maintain and any WordPress developer that has experience with register_post_type() will instantly know what’s going on. Whereas if a library or plugin is used to register the CPTs, it increases the amount of unfamiliar code that needs to be combed through to figure out what exactly is happening.

For those reasons I’ve always refrained from building an abstraction layer, despite my frequent use of CPTs and custom taxonomies.

However, it’s still a minor hassle to manually register CPTs, write out all the necessary strings, and add standard functionality. That’s why I went ahead and put together a few Mustache templates to generate a little code to help speed things up and create more consistency across my projects.

I thought others might find the templates helpful as well, so I’ve published a couple of forms for interacting with them. The idea is to improve the quality of code by highlighting some best practices and coding standards, potentially introduce functionality you may not be familiar with, and demonstrate how it all works together — and ultimately, save some time!

I hope you find them useful.

You can find the WordPress code generators for CPTs and custom taxonomies here and an example of the code generated can be seen in this Gist.

Comments

Leave a Reply

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