Custom Post Type Generator
Generate register_post_type() code with labels, supports, REST and rewrite settings.
<?php
/**
* Registers the "book" post type.
*/
function my_plugin_register_book() {
$labels = array(
'name' => _x( 'Books', 'Post type general name', 'my-plugin' ),
'singular_name' => _x( 'Book', 'Post type singular name', 'my-plugin' ),
'menu_name' => _x( 'Books', 'Admin Menu text', 'my-plugin' ),
'add_new_item' => __( 'Add New Book', 'my-plugin' ),
'edit_item' => __( 'Edit Book', 'my-plugin' ),
'all_items' => __( 'All Books', 'my-plugin' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-book',
'supports' => array( 'title', 'editor', 'thumbnail' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'my_plugin_register_book' );
Output is valid and updates as you type.
Fix the highlighted fields to update the output.
Build the register_post_type() call for a custom post type without memorising every argument. Pick labels, visibility, supported features and permalinks, then copy code that follows WordPress Coding Standards.
How to use
- Enter a post type key (20 characters or fewer, lowercase) and a function name prefixed with your plugin slug.
- Set the singular and plural labels and your text domain.
- Choose visibility, supported features and, under More options, permalinks and taxonomies.
- Copy the code into your plugin’s main file or an included file.
Example
A “book” post type with an archive and block editor support:
register_post_type( 'book', array(
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
) );
Pitfalls
- Post type keys longer than 20 characters are rejected by WordPress.
- Register on the
inithook; registering earlier breaks rewrite rules. - Flush permalinks once after adding the post type, or archive URLs return 404.
- Without
show_in_rest, the block editor falls back to the classic editor.
Compatibility
show_in_restrequires WordPress 4.7 or newer; the block editor needs 5.0 or newer.- The generated code runs on PHP 7.4 and newer.