WordPress Custom Post Type & Meta Field Code Exporter
Generate clean, security-hardened register_post_type and custom meta field PHP code. Eliminate database overhead and plugin dependency with production-ready snippets for functions.php or mu-plugins.
Interactive Schema & Meta Architect
Configure labels, REST routing, and custom meta fields. Code updates in real time.
<?php
/**
* Programmatic Post Type Registration: Case Studies
* Optimized for PHP 8.2+ and WordPress VIP coding standards.
* Zero external plugin dependencies. Drop into wp-content/mu-plugins/cpt-case_study.php
*/
declare(strict_types=1);
add_action('init', function(): void {
$labels = [
'name' => _x('Case Studies', 'Post Type General Name', 'sheharyar'),
'singular_name' => _x('Case Study', 'Post Type Singular Name', 'sheharyar'),
'menu_name' => __('Case Studies', 'sheharyar'),
'name_admin_bar' => __('Case Study', 'sheharyar'),
'archives' => __('Case Study Archives', 'sheharyar'),
'attributes' => __('Case Study Attributes', 'sheharyar'),
'parent_item_colon' => __('Parent Case Study:', 'sheharyar'),
'all_items' => __('All Case Studies', 'sheharyar'),
'add_new_item' => __('Add New Case Study', 'sheharyar'),
'add_new' => __('Add New', 'sheharyar'),
'new_item' => __('New Case Study', 'sheharyar'),
'edit_item' => __('Edit Case Study', 'sheharyar'),
'update_item' => __('Update Case Study', 'sheharyar'),
'view_item' => __('View Case Study', 'sheharyar'),
'view_items' => __('View Case Studies', 'sheharyar'),
'search_items' => __('Search Case Study', 'sheharyar'),
'not_found' => __('Not found', 'sheharyar'),
'not_found_in_trash' => __('Not found in Trash', 'sheharyar'),
];
$args = [
'label' => __('Case Study', 'sheharyar'),
'description' => __('High performance programmatic Case Study entity', 'sheharyar'),
'labels' => $labels,
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'revisions'],
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-portfolio',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => 'case-studies',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rest_base' => 'case-studies',
'rewrite' => [
'slug' => 'case-studies',
'with_front' => false,
],
];
register_post_type('case_study', $args);
}, 0);
/**
* Register Security-Hardened Native Meta Box
*/
add_action('add_meta_boxes', function(): void {
add_meta_box(
'case_study_metadata_box',
__('Case Study Specifications', 'sheharyar'),
'render_case_study_meta_box',
'case_study',
'normal',
'high'
);
});
function render_case_study_meta_box(\WP_Post $post): void {
wp_nonce_field('case_study_meta_action', 'case_study_meta_nonce');
?>
<div style="display:grid; grid-template-columns: 1fr; gap: 14px; padding: 10px 0;">
<div>
<label for="client_organization" style="font-weight:600; display:block; margin-bottom:4px;">
Client Organization
</label>
<input type="text"
id="client_organization"
name="client_organization"
value="<?php echo esc_attr(get_post_meta($post->ID, '_client_organization', true)); ?>"
style="width: 100%; max-width: 500px;" />
</div>
<div>
<label for="project_budget_usd" style="font-weight:600; display:block; margin-bottom:4px;">
Project Budget USD
</label>
<input type="number"
id="project_budget_usd"
name="project_budget_usd"
value="<?php echo esc_attr(get_post_meta($post->ID, '_project_budget_usd', true)); ?>"
style="width: 100%; max-width: 500px;" />
</div>
<div>
<label for="architecture_blueprint" style="font-weight:600; display:block; margin-bottom:4px;">
Architecture Blueprint Image
</label>
<input type="text"
id="architecture_blueprint"
name="architecture_blueprint"
value="<?php echo esc_attr(get_post_meta($post->ID, '_architecture_blueprint', true)); ?>"
style="width: 100%; max-width: 500px;" />
</div>
<div>
<label for="outcome_metrics" style="font-weight:600; display:block; margin-bottom:4px;">
Verified Outcome Metrics
</label>
<input type="text"
id="outcome_metrics"
name="outcome_metrics"
value="<?php echo esc_attr(get_post_meta($post->ID, '_outcome_metrics', true)); ?>"
style="width: 100%; max-width: 500px;" />
</div>
</div>
<?php
}
/**
* Save Meta Box Data with Strict Nonce and Permission Verification
*/
add_action('save_post_case_study', function(int $post_id, \WP_Post $post, bool $update): void {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['case_study_meta_nonce']) || !wp_verify_nonce((string)$_POST['case_study_meta_nonce'], 'case_study_meta_action')) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['client_organization'])) {
update_post_meta($post_id, '_client_organization', sanitize_text_field((string)$_POST['client_organization']));
}
if (isset($_POST['project_budget_usd'])) {
update_post_meta($post_id, '_project_budget_usd', sanitize_text_field((string)$_POST['project_budget_usd']));
}
if (isset($_POST['architecture_blueprint'])) {
update_post_meta($post_id, '_architecture_blueprint', sanitize_text_field((string)$_POST['architecture_blueprint']));
}
if (isset($_POST['outcome_metrics'])) {
update_post_meta($post_id, '_outcome_metrics', sanitize_text_field((string)$_POST['outcome_metrics']));
}
}, 10, 3);
Executive Summary & Direct Answer:
Registering custom post types and custom meta fields directly in PHP code eliminates the database query overhead, security attack surface, and serialization bugs introduced by UI generator plugins while ensuring 100% version control and instant opcode cache execution.
- Executing programmatic registrations in mu-plugins cuts wp_options query load during bootstrap.
- Native custom meta boxes protect your editorial workflow with explicit nonces and user permission barriers.
- Exporting ACF local PHP field groups stops database field lookups completely.
- Setting show_in_rest to true allows immediate decoupling for modern React frontends or Gutenberg blocks.
Why programmatic post type registration outperforms UI generator plugins
In many WordPress audits I perform, site speed issues begin with how content types are registered. Site builders often rely on plugins like Custom Post Type UI or heavy all-in-one frameworks to create portfolio items, team members, or service cards.
These plugins store their schema definitions inside serialized option rows in wp_options. On every single web request, WordPress must query the database, unserialize the arrays, and pass them into register_post_type. When a site scales to dozens of content types, this adds unnecessary database seek time before template files even load.
Writing your registration logic in pure PHP eliminates that step entirely. Once compiled by PHP OPCache, your post types and meta fields load straight from server memory in fractions of a millisecond. Furthermore, your schema lives cleanly in Git repositories, making staging-to-production migrations completely deterministic.
Why mu-plugins are superior to theme functions.php
Placing your custom post types inside your theme functions.php file creates an unnecessary content lock-in. The moment you switch or rebuild your theme, all your custom post types disappear from the admin menu.
By placing the generated file in wp-content/mu-plugins/, your data architecture remains completely independent of your presentation layer. Must-use plugins cannot be accidentally deactivated by clients in the WordPress dashboard.
Security-hardened meta save routines
Vulnerabilities in custom post meta handling almost always stem from two oversights: missing nonces and inadequate permission verification.
The code generated by this tool implements strict wp_verify_nonce checks, verifies current_user_can edit permissions, and prevents execution during automated autosaves before running sanitize_text_field calls.
Frequently Asked Questions on Custom Post Types
Developer specifics regarding REST endpoints, permalink flushes, and meta serialization.
Need complex content architectures or custom plugins built?
I engineer decoupled backends, custom REST API endpoints, high-concurrency database optimizations, and bespoke enterprise WordPress plugins for digital agencies and growth brands.