Skip to: Content or Footer

WordPress, add custom post type template during registration

Created on
No updates
Approx ~6 minutes reading time for 1,231 words.

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;

  1. core/heading
    With level and placeholder attributes.
  2. core/paragraph
    With placeholder and an optional lock which disallows moving or removing.
  3. core/image
    With an align attribute.
  4. core/heading
    Again with a level but this time set to 2 and content instead of placeholder.

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 block.json file, and look at the attributes and supports values.

WordPress Codex

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.

Before committing to a template layout, especially if it’s more than a few blocks I’d highly recommend adding a few posts as a test to ensure it’s all 100% OK. If you know there’s going to be lots of posts using this template it’ll pay to check beforehand otherwise you’ll have a lot of manual edits to do later.

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

Category: Technical
Topics: #Tech-Stack, #WordPress

Dave Barr

Scottish Expat who has 20+ years experience of Web Development and is continually on the look out to improve his skill sets. Learning new and innovative solutions for current requirements in the world of IT, Web Development and eCommerce.

About Dave Barr

Scottish Expat who has 20+ years experience of Web Development and is continually on the look out to improve his skill sets. Learning new and innovative solutions for current requirements in the world of IT, Web Development and eCommerce.

Read more about Dave

Back to Top

Click to Copy