Scaffolding Custom Post Types and Taxonomies

Custom post types and custom taxonomies are the building blocks for advanced functionality. Most of the time, the code for registering them is either copy+pasted from somewhere or it’s saved as a snippet in your text editor.

Now you can do this with WP-CLIWP-CLI WP-CLI is the Command Line Interface for WordPress, used to do administrative and development tasks in a programmatic way. The project page is http://wp-cli.org/ https://make.wordpress.org/cli/, using the wp scaffold command.

Scaffold a custom post typeCustom Post Type WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Custom Post Types gives your site the ability to have templated posts, to simplify the concept.

The following command generates the code for registering the post type and also for customizing the update messages:

wp scaffold post-type zombie

Scaffold a taxonomyTaxonomy A taxonomy is a way to group things together. In WordPress, some common taxonomies are category, link, tag, or post format. https://codex.wordpress.org/Taxonomies#Default_Taxonomies.

The following command generates the code for registering the taxonomy to the zombie post type:

wp scaffold taxonomy zombie-speed --post_types=zombie

But I only want the registration of the post-type…

Then you can add the --raw flag:

wp scaffold post-type zombie --raw

This will only output the code that will register_post_type() without the init action and the update messages.

But where does the code go?

This is a personal preference. Some ship it within a theme, while others will save it within a pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party. That’s why we have these two flags:

wp scaffold post-type zombie --theme

--theme stores the generated code within your current active theme under a /post-types directory and names a file name zombie.php

wp scaffold post-type zombie --plugin=pluginname

--plugin=plugin-dir-name stores the generated code within the given plugin name.

By default, the code will be output to your terminal (STDOUT) so you could write it to your file of choice:

wp scaffold post-type zombie > ~/project/foo/bar.php

And what about I18n?

Internationalization by gettext can be achieved through the --textdomain parameter:

wp scaffold post-type zombie --textdomain=my-textdomain

By default when using the --theme, the textdomain will be that of your active theme, and if
using the --plugin flag, the textdomain will be your plugin name.

Enjoy these commands; we hope it will speed-up and ease your development with WordPress!