PEN BEAM / COPY-READY EXAMPLE
Register a Custom Post Type
A minimal WordPress custom post type with REST support, an archive and readable rewrite URLs.
What this snippet does
Registers a custom post type that can appear in the REST API and have a public archive. Use a unique, project-specific machine name and slug.
PHP / source code
add_action('init', 'pb_example_register_resource_type');
function pb_example_register_resource_type(): void {
register_post_type('pb_example_resource', [
'labels' => [
'name' => __('Resources', 'your-text-domain'),
'singular_name' => __('Resource', 'your-text-domain'),
],
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'example-resources', 'with_front' => false],
'menu_position' => 20,
'menu_icon' => 'dashicons-media-document',
'supports' => ['title', 'editor', 'excerpt', 'thumbnail', 'revisions'],
]);
}
Where to place it
Place the registration in a small site-specific plugin when the content type must remain available if the active theme changes. For a theme-only project, use the child theme functions.php and document the dependency.
Back up the relevant file or settings first. Apply the change on staging, verify behavior, and retain a rollback path before production deployment.
What to check before deploying
- Flush permalinks by saving Settings → Permalinks once after deployment; never call flush_rewrite_rules() on every page load.
- Confirm the archive and single URLs resolve and no existing page or post slug conflicts with the chosen rewrite slug.
- Check REST permissions and fields, add suitable capabilities if needed, and decide how the data remains available if the theme or code is removed.