Where tools run
In your browser
Sent to a server
Nothing you type
Sign-up needed
None
Share link settings
In the URL hash

Custom Post Type Generator

The Custom Post Type Generator builds the register_post_type() call for a custom post type: labels, visibility, supported features, REST and permalinks, in code that follows the WordPress Coding Standards.

Step by step

  1. Enter a post type key of 20 characters or fewer, in lowercase, and a function name prefixed with your plugin slug.
  2. Set the singular and plural labels and your text domain.
  3. Choose visibility, supported features and, under More options, permalinks and taxonomies.
  4. Copy the code into your plugin’s main file or a file it includes.

What you get

my-plugin.php
<?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' );

Things to know

  • Register on the init hook; registering earlier breaks rewrite rules.
  • Visit Settings, 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.
  • Put the code in a plugin, not a theme, so the content stays available if the theme changes.