Register WordPress post types and taxonomies using object-orientated design.
Most implementation of registering post types and taxonomies in WordPress might use classes, but these are little more than poor namespaces. Multiple post types and taxonomies are either all registered via methods in the same class, or contain duplicate code across multiple classes. I wanted to dive into how registration could be structured using something that is closer to real OOP.
The code seen here is in working use on a live project.
Gamajo_Registerable- interface for different things that are registerable. Methods include
Gamajo_Post_Type- abstract class for registering post types. Implements
Gamajo_Registerable, so required methods say how a post type should be registered, unregistered and how arguments should be handled. Includes abstract methods for
messages()which will contain implementation for specific post types.
Gamajo_Taxonomy- abstract class for registering taxonomies. Implements
Gamajo_Registerable, so required methods say how a taxonomy should be registered, unregistered and how arguments should be handled. Includes abstract methods for
default_args()which will contain implementation for specific taxonomies. There is a little duplication here between this and
Gamajo_Post_type- either the
Gamajo_Registerablecould be changed from an interface to an abstract class to accommodate the common method implementations, or traits could be used if the minimum PHP version was increased from 5.2.
Meal_Planner_Post_Type_Recipe- specific implementation of a post type, which extends
Gamajo_Post_Type. Only defines the
$post_typeproperty, and the
Meal_Planner_Taxonomy_Recipe_Type- specific implementation of a taxonomy, which extends
Gamajo_Taxonomy. Only defines the
$taxonomyproperty, and the
If you register multiple post types and taxonomies, you can see that only multiple classes that extend the abstract classes are needed, with specific details, creating immutable objects. The boilerplate of registering, unregistering and handling arguments don't need to be repeated.
- PHP 5.2+
This isn't a WordPress plugin on its own, so the usual instructions don't apply. Instead:
- Copy the
gamajofiles into your plugin. It can be into a file in the plugin root, or better, an
- Ensure the classes and interfaces are available. You can
require_onceeach file, if the structural element doesn't exist, or just use an autoloader (renaming anything as needed if following PSR-0/4).
meal-planner files are examples - concrete objects of a post type and taxonomy. The only thing you need to populate for each new post type or taxonomy are the default args, and the messages. At this point, we've only created a specific post type or taxonomy class - since the class isn't instantiated, WordPress won't actually register anything.
@todo Information about how to set up a registration handler.
Issues and Pull Requests welcomed against the 'develop' branch. All code should follow the WordPress coding standards and be fully documented.
GPL-2.0+, so feel free to amend and use in any personal or commercial projects.