I found WordPress extremely useful as base for medium sized web apps, without too much traffic. Building a small application, for example a restaurant table booking system, from scratch or even with a framework like Rails or CakePHP, would involve thinking about database scheme, controller structure, authentication, user interfaces etc. A lot of this stuff WordPress is already doing: you already have rough user management, a working admin interface and you only have to think about how to map your data-model to the already existing WordPress database structure.
Our use case: A recipe database
Now, I want to show you how to implement a simple recipe database with WordPress. Requirements are really basic:
- Allows adding recipes and editing them just like ordinary posts or pages
- Allows categorizing recipes in hierarchical categories like Healthy -> Chicken -> Marinated Chicken Breasts
- Allows adding ingredients to a recipe and finding recipes by ingredients
- Allows adding quantities to ingredients of a recipe, e.g. 500ml milk, 20g sugar, 3 tablespoons olive oil
I will focus on how to customize WordPress to adjust it to your data model, mainly by showing you how to use the essentials to build every WordPress powered web app:
- Custom post types
- Custom taxonomies
- Custom fields
In WordPress the base of every content holding entity is a post. The two post types you surely are familiar with are posts and pages. For our recipe database we will create a new type: recipes.
register_post_type('recdb_recipe', array( 'labels' => array( 'name' => 'Recipes', ), 'public' => true, 'has_archive' => true, 'supports' => array('title','editor','custom-fields') ) );The new post type will show up in the admin interface along with posts and pages. It supplies a field for the recipe title and a editor field which is meant for the recipes description part. We even added support for custom fields, which will be necessary later on.
Setup custom taxonomies for recipe categories and ingredients
In WordPress, taxonomies are not for holding content, like posts, they are for organizing it. You should be used to the two default taxonomies: categories (hierarchical) and tags (not hierarchical). However, like with post types, you can add your own kinds of taxonomies. For our recipes database you could use categories and tags, but they are meant to be used for posts really, so we create our own recipe categories and link them to the recipe post type:
register_taxonomy( 'recdb_categories', array('recdb_recipe'), array( 'label' => 'Recipe categories', 'sort' => true, 'rewrite' => array('slug' => 'recipe-categories'),