WordPress, add custom post type template during registration
Introduction
If you’re creating a WordPress Custom Post Type (CPT
) that’s going to be fairly uniform in its layout structure you could have a look at adding a template.
In the article I’ll be referring to John Billion’s Extended CPTs as that’s the method I use for registering CPTs
.
Extended CPTs is a library which provides extended functionality to WordPress custom post types and taxonomies. This allows developers to quickly build post types and taxonomies without having to write the same code again and again.
John Billion, GitHub Repo
What is a CPT template?
A template is defined as a list of block items. These blocks can have predefined attributes, placeholder content, static content and be locked or dynamic.
Why use Extended CPTs?
It’s a major time saver and I also find it much easier to maintain. Essentially it’s a wrapper for the WordPress register_post_type()
function. For a full in-depth look, check out the GitHub Wiki Documentation.
The code
Below is the code I’ll be referring to where the template array
is wrapped in the register_extended_post_type()
function.
add_action(
'init',
function() {
register_extended_post_type(
'my-custom-post-type',
array(
...
'template' => array(
array(
'core/heading',
array(
'level' => '1',
'placeholder' => 'Add Title...',
),
),
/**
* LOCK Do not allow paragraph to be
* moved or removed
*/
array(
'core/paragraph',
array(
'placeholder' => 'Add Description...',
'lock' => array(
'move' => true,
'remove' => true,
),
),
),
array(
'core/image',
array(
'align' => 'left',
),
),
array(
'core/heading',
array(
'level' => '2',
'content' => 'Introduction',
),
),
),
),
...
);
}
);
Included in this simple example we have;
core/heading
Withlevel
andplaceholder
attributes.core/paragraph
Withplaceholder
and an optionallock
which disallows moving or removing.core/image
With analign
attribute.core/heading
Again with alevel
but this time set to2
andcontent
instead ofplaceholder
.
The possibilities are endless
By combining the power of Extended CPTs
, and adding a template
in the registration process you can create any type of layout you can think of. You can even nest blocks within blocks.
What attributes are available for each block type?
To find a comprehensive list of all block attributes that you can define in a template, consult the block’s
WordPress Codexblock.json
file, and look at theattributes
andsupports
values.
To locate a block that you are interested in finding out more about, go to its directory and open the block.json
file;
wp-includes/blocks/foo/block.json
For example…
Using the three core
blocks, heading, paragraph and image (from above) as an example here are links to the GitHub source for each.
You can even lock an entire template down
It’s relatively straightforward to lock down your template
with the template_lock
argument. You could then conditionally release certain blocks with the lock
attribute by setting move
and remove
to false
.
array( 'core/paragraph',
array(
'placeholder' => 'Add Description...',
'lock' => array(
'move' => false,
'remove' => false,
),
),
),
Long story short there’s a huge amount you can do to build a template
uniquely suited to any purpose.
In conclusion
Using this approach has saved me lots of time. Not having to create Advanced Custom Field (ACF
) Groups and building a custom template for the single-posts
with all the additional overhead that produces. Using a template is a faster, maintainable, ‘native’ way of handling non complex CPT
layouts.
Add in utilising Extended CPTs
for CPT
creation and we have a quick to setup, easily maintainable combination.
// End of Article
Article Information
Topics: #Tech-Stack, #WordPress
Further Reading
- Advanced Custom Fields Website (advancedcustomfields.com)
- Extended CPTs GitHub Repo (github.com)
- Extended CPTs GitHub Wiki Docs (github.com)
- WordPress Block Template Docs (developer.wordpress.org)
- WordPress register_post_type() Function (developer.wordpress.org)