Structuring custom domain models—such as Portfolios, Case Studies, Testimonials, Team Directories, or Events—without heavy database plugins keeps WordPress sites fast and maintainable. The CodeToWP WordPress CPT Generator gives WordPress developers, agencies, and freelancers instant, dependency-free PHP boilerplate combining register_post_type(), optional taxonomies, and nonce-protected meta boxes.
Who the WordPress CPT Generator Is Built For
Visual CPT UI plugins store post type configurations in WordPress options tables, couple schema management to admin dashboards, and make Git-based code reviews harder. Writing full register_post_type() label arrays, taxonomy registrations, and nonce-checked save_post handlers manually is repetitive and error-prone.
This WordPress CPT Generator compiles readable, handbook-compliant PHP code in seconds—ready for your theme’s inc/cpt.php or a site-specific companion plugin without plugin lock-in.
Features, Settings & Supported Outputs
- Sample Presets & Identity Fields: Load Portfolio Projects, Client Testimonials, or Team Members presets, or configure Singular Label, Plural Label, Post Type Key slug, and Menu Icon Dashicon class (defaults to
dashicons-portfolio). - Supported Feature Checkboxes & Behavior Toggles: Toggle post-type features (
title,editor,thumbnail,excerpt,custom-fields,revisions) plus switches for Gutenberg/REST API support (show_in_rest), Hierarchical (Page-like parent/child hierarchy), companion hierarchical Taxonomy registration, and the Custom Meta Box Builder. - Interactive Custom Meta Box Builder: Click + Add Custom Field to append fields supporting four input types (
text,url,number, andtextarea), complete with WordPress nonces (wp_nonce_field()), autosave protection,current_user_can()checks, and type-matched sanitization (sanitize_text_field,esc_url_raw,intval,sanitize_textarea_field). - Direct Export: Copy the compiled PHP or download
cpt-{slug}.php.
Practical Example: Portfolio CPT with Taxonomy & Client Field
Sample Tool Settings
Singular: Portfolio | Plural: Portfolio Items
Slug: portfolio | Dashicon: dashicons-portfolio
Toggles: show_in_rest (ON), Include Taxonomy (ON), Custom Meta Box (ON)
Custom Field: Label = "Client Name", ID = "client_name", Type = "text"
Expected Generated PHP Output Excerpt
function codetowp_register_cpt_portfolio() {
register_post_type( 'portfolio', array(
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_icon' => 'dashicons-portfolio',
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
) );
register_taxonomy( 'portfolio_category', array( 'portfolio' ), array(
'hierarchical' => true,
'show_in_rest' => true,
'show_admin_column' => true,
) );
}
add_action( 'init', 'codetowp_register_cpt_portfolio' );
Result Explanation: The output registers portfolio and portfolio_category on init following the official WordPress register_post_type() reference, plus nonce-checked _client_name saving on save_post_portfolio.
Practical Use Cases
- WordPress Developers: Version-control clean CPT schemas inside custom plugins or starter themes with zero plugin overhead.
- Frontend Developers: Set up custom post endpoints (
/wp-json/wp/v2/{slug}) quickly for Gutenberg or headless data modeling. - Agencies: Standardize repeatable Portfolio, Testimonial, Case Study, and Team modules across client retainers.
- Freelancers: Add structured metadata inputs (client URLs, ratings, bios) in classic admin meta boxes without purchasing custom-field suites.
Limitations & Compatibility
- Manual 20-Character Key & Quote Discipline: While WordPress core enforces a 20-character maximum on post type keys (lowercase alphanumeric and underscores only), the Slug input field does not automatically block longer keys or special characters—verify your slug stays 20 characters or fewer and avoid apostrophes/quotes inside labels or field names.
- Fixed Architectural Defaults: Output defaults to prefix
codetowp_, text domain'codetowp',public => true,has_archive => true,menu_position => 20, and a hierarchical taxonomy (`{singular} Category`). Adjust these keys directly in PHP if you need private post types or non-hierarchical tags. - Meta Box REST Exposure & Persistence: Generated meta boxes work cleanly in the WordPress admin editor (including block editor screens) and save sanitized
_{id}post meta, but do not automatically emitregister_post_meta(..., ['show_in_rest' => true]); note also that the Supports checkboxes revert to defaults after a full browser reload, whereas labels and custom meta rows persist inlocalStorage. - Compatibility: Compatible with WordPress 6.0–6.7+ and PHP 8.0–8.3+.
Next Steps & Related CodeToWP Tools
Flush rewrite rules under Settings > Permalinks > Save Changes after activating your WordPress CPT Generator code file, pair your custom schema with a starter theme from our Starter Theme ZIP Generator or custom cards via the HTML to Elementor Widget Builder, and read Building Custom Post Types and Taxonomies in WordPress.