wp scaffold taxonomy
Overview
Generate the register_taxonomy() PHP code for a custom taxonomy — with all labels, rewrite rules, REST API support, and post type association configured. Output directly to a plugin file or review it first in the terminal.
What It Does
wp scaffold taxonomy outputs a valid, complete register_taxonomy() function call that you can drop directly into your plugin or theme.
Syntax
wp scaffold taxonomy <slug> [OPTIONS]
Options
| Flag | Description |
|---|---|
SLUG | Taxonomy key (e.g., genre, location) |
--post_types=TYPES | Comma-separated post types to associate the taxonomy with |
--label=LABEL | Human-readable label (singular) |
--textdomain=TD | Text domain for i18n |
--theme | Write code to the active theme's functions.php |
--plugin=PLUGIN | Append code to a plugin file |
--force | Overwrite existing |
Basic Usage
wp scaffold taxonomy genre \
--post_types=book \
--label="Genre" \
--textdomain=my-plugin
Output (to terminal):
<?php
/**
* Registers the `genre` taxonomy,
* for use with 'book'.
*/
function genre_init() {
register_taxonomy( 'genre', [ 'book' ], [
'hierarchical' => false,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => true,
'labels' => [
'name' => __( 'Genres', 'my-plugin' ),
'singular_name' => __( 'Genre', 'my-plugin' ),
'search_items' => __( 'Search Genres', 'my-plugin' ),
'popular_items' => __( 'Popular Genres', 'my-plugin' ),
'all_items' => __( 'All Genres', 'my-plugin' ),
'edit_item' => __( 'Edit Genre', 'my-plugin' ),
'update_item' => __( 'Update Genre', 'my-plugin' ),
'add_new_item' => __( 'Add New Genre', 'my-plugin' ),
'new_item_name' => __( 'New Genre Name', 'my-plugin' ),
'not_found' => __( 'No genres found.', 'my-plugin' ),
],
'show_in_rest' => true,
'rest_base' => 'genre',
'rest_controller_class' => 'WP_REST_Terms_Controller',
] );
}
add_action( 'init', 'genre_init' );
Write to Plugin
wp scaffold taxonomy location \
--post_types=portfolio \
--label="Location" \
--plugin=my-portfolio-plugin
Best Practices
- Use
show_in_rest=true(set by scaffold default) so the taxonomy works in Gutenberg. - Consider
hierarchical—truefor category-like taxonomies,falsefor tag-like. - Pair with a CPT created by
wp scaffold post-type.
Quick Reference
wp scaffold taxonomy <slug> --post_types=<type> --plugin=<plugin>
Next Steps
wp scaffold post-type— create a CPT to attach this taxonomy to.wp term create— add terms to the new taxonomy.