Table of Contents
Table of Content
Create a Custom Post Type (CPT) for WordPress #
After you have created a plugin you can now define and configure your custom post type (CPT).
In the following example we will create a new Post Type for "books".
- Add a folder
Configuration/CPT
to your plugin. - Create a file
book.php
in the folderConfiguration/CPT
. You can name the file however you like – in this example we want to create a CPT for books, so the name kind of makes sense 😉 - Add the following script to the
book.php
:
File: Configuration/CPT/book.php
#
<?php
$cptConfig = [
'labels' => [
'name' => 'Books',
'singular_name' => 'Book',
],
'description' => 'List of books from the example extension.',
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-book',
'menu_position' => 20,
'supports' => ['title', 'editor', 'custom-fields', 'thumbnail'],
'rewrite' => ['slug' => 'example'],
'taxonomies' => ['category'],
];
return [
'config' => $cptConfig,
'forms' => []
];
That was it. You now have a Custom Post Type (CPT) which you can add entries to in the backend.
Information about the options in the CPT-configuration can be found in the WordPress Documentation
Convention over Configuration #
- The
post_type
of your CPT will be stored in the database using the convention{pluginname}_{cptname}
. If your plugin-folder is namedexample
and the CPT-file is namedbook.php
, then thepost_type
in the database will beexample_book