Learn how to generate a new Custom Post Type (CPT) in any Genesis Framework child theme using code.
It’s true that to create a custom CPT you can use a plugin such as Custom Post Custom Post Type UI, or even something more specific, such as Portfolio Post Type.
However, this is Osom Code. 😉
So, true to our name, today you’re going to discover how to do the same thing using a few lines of code.
How?
By generating your own Plugin, a tool with 2 great advantages over any other Plugin in the WordPress repository:
- It will be 100% customized to your needs.
- It will be much lighter.
In fact, both are closely related.
Less lines of code –> Less weight
Snippet to create a CPT in Genesis Framework
To get your own plugin, generate a file called, for example, “cpt-portfolio.php” with the following snippet and put it inside a folder with the same name (in this case it would be “cpt-portfolio”):
To see this and another 1097 code snippets of this website, login or subscribe here.
When you place this folder in the path /wp-content/plugins/ a plugin will appear in the WordPress panel that you can activate from there (just like any other plugin).
Customization options in the plugin
If you use the above fragment of code without applying any changes, you get a Custom Post Type of portfolio. But you know you can replace it with the one you need (events, podcasts, courses…).
In addition, this plugin also records a taxonomy called “Category”, which allows you to categorize the entries of the CPT you just created.
You don’t need it and you prefer to lighten it up even more?
That’s fine. Just delete this fragment:
To see this and another 1097 code snippets of this website, login or subscribe here.
Finally, the lines you see at the beginning make up the plugin header. There is all the information that will appear in the WordPress administration panel.
I invite you to edit it to reflect what your version of the plugin does.
Other considerations
Although this plugin works with any WordPress child theme, I have made sure that the code supports all the necessary Genesis attributes, indicated in this line:
'supports' => array( 'title', 'editor', 'author', 'excerpt', 'thumbnail', 'custom-fields', 'genesis-cpt-archives-settings', 'genesis-seo', 'genesis-scripts', 'genesis-layouts', 'genesis-rel-author', 'genesis-breadcrumbs-toggle', 'genesis-footer-widgets-toggle', 'genesis-title-toggle', 'genesis-singular-images' ),
This way, your Custom Post Type will show all the metaboxes with the Genesis settings (design, SEO, scripts…).
As if that wasn’t enough, the CPT will also be compatible with the WordPress block editor.
Update: The code has been updated to support the new Genesis sidebar options for the block editor, introduced in version 3.1.
Conclusion
Here is the structure of a lightweight plugin to create your own custom CPTs that are 100% compatible with Genesis.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂