wp scaffold post-type
Overview
Generate the register_post_type() PHP code for a custom post type — with all labels, capabilities, rewrite rules, and REST API support properly configured. Paste it directly into your plugin or theme and customise from there.
What It Does
wp scaffold post-type outputs a ready-to-use register_post_type() function call based on the options you provide. It can write the code directly to a file or output it to stdout for review.
Syntax
wp scaffold post-type <slug> [OPTIONS]
Options
| Flag | Description |
|---|---|
SLUG | Post type key (e.g., book, portfolio) |
--label=LABEL | Human-readable label (singular) |
--textdomain=TD | Text domain for translations |
--dashicon=ICON | Admin menu dashicon |
--theme | Output to the active theme's functions.php |
--plugin=PLUGIN | Plugin file to append generated code to |
--force | Overwrite existing files |
Basic Usage
wp scaffold post-type book \
--label="Book" \
--textdomain=my-plugin \
--dashicon=book
Output (printed to terminal):
<?php
/**
* Registers the `book` post type.
*/
function book_init() {
register_post_type( 'book', [
'labels' => [
'name' => __( 'Books', 'my-plugin' ),
'singular_name' => __( 'Book', 'my-plugin' ),
'all_items' => __( 'All Books', 'my-plugin' ),
'archives' => __( 'Book Archives', 'my-plugin' ),
'add_new' => __( 'Add New', 'my-plugin' ),
'add_new_item' => __( 'Add New Book', 'my-plugin' ),
'edit_item' => __( 'Edit Book', 'my-plugin' ),
'new_item' => __( 'New Book', 'my-plugin' ),
'view_item' => __( 'View Book', 'my-plugin' ),
'search_items' => __( 'Search Books', 'my-plugin' ),
...
],
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'supports' => [ 'title', 'editor' ],
'has_archive' => true,
'rewrite' => [ 'slug' => 'book' ],
'query_var' => true,
'menu_icon' => 'dashicons-book',
'show_in_rest' => true,
'rest_base' => 'book',
'rest_controller_class' => 'WP_REST_Posts_Controller',
] );
}
add_action( 'init', 'book_init' );
Write Directly to Plugin
wp scaffold post-type portfolio \
--label="Portfolio" \
--textdomain=my-portfolio \
--plugin=my-portfolio-plugin
Best Practices
- Review generated labels — WP-CLI generates all 20+ labels automatically; confirm they read naturally.
- Enable REST API —
show_in_rest => trueis set by default, enabling Gutenberg support. - Add
supportsbased on your needs (addthumbnail,custom-fields, etc.).
Quick Reference
wp scaffold post-type <slug> --label=<label> --plugin=<plugin>
Next Steps
wp scaffold taxonomy— create taxonomy for the post type.wp post create— create posts of the new type.