Class for registering post types
We will tackle the registration of CPT(custom post type) in a more OOP way.
Create a new file class-custom-post-types.php in the same directory where functions.php is located in the theme folder.
Inside the file paste the following code.
We used the class constructor to call Wordpress to init action with a callback to our
register_post_types method. Inside the method we are using wp built-in function, to register
our CPT.
Register_post_type function excepts a number of parameters, but for now, we used
only minimal requirements to make this work, we will extend it later.
Include class
The only thing left is to include our class in function.php. Copy the following code to functions.php with this in place you are all set for using your own CPT.
Here we first required our file and then created an object of the class custom post types.
Internationalization
Currently, our CPT doesn't support translations. The name My Cpt would stay the same in all wp installations. To fix that we need to update our register_post_type, to support text-domain.
We used localization function (__), so the string My Cpt could be translated into other languages.
Additional parameters for register cpt
You can add additional parameters for registering CPT based on your needs. Take a look at
Wordpress docs
We will extend our CPT with description, slug and parameters to support Gutenberg.