Skip to main content

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

FlagDescription
SLUGPost type key (e.g., book, portfolio)
--label=LABELHuman-readable label (singular)
--textdomain=TDText domain for translations
--dashicon=ICONAdmin menu dashicon
--themeOutput to the active theme's functions.php
--plugin=PLUGINPlugin file to append generated code to
--forceOverwrite 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

  1. Review generated labels — WP-CLI generates all 20+ labels automatically; confirm they read naturally.
  2. Enable REST APIshow_in_rest =&gt; true is set by default, enabling Gutenberg support.
  3. Add supports based on your needs (add thumbnail, custom-fields, etc.).

Quick Reference

wp scaffold post-type <slug> --label=<label> --plugin=<plugin>

Next Steps