Create Custom Post Type for WordPress Plugin

What is a Custom Post Type?

I like to think of a custom post type as an object specifically as a different version of the WordPress Post object. You get all of the benefits of the post object but can name the object something else to suit your particular purposes.  We created a custom post type called Products for our WPMerchant WordPress plugin. We wanted a way to create products and add a cost to the product and charge a customer for that product. Creating a Products custom post type was the best way to do that. You can see the Products link in the image below.

When you click on Products, you see a list of all of the WPMerchant Products that have been created.

If you click on a particular product, you’ll go to the WPMerchant Product Edit Page.

Important Note:

As a reminder, we’re using the WordPress Plugin Boilerplate to build this plugin. If you have never used this WordPress plugin framework, check out our post on WordPress Plugin Development. You’re going to open the class-plugin-name-admin.php file that exists within the admin directory (in your WordPress Plugin Boilerplate framework).

STEP 1: Add to the class-plugin-name-admin.php file’s construct function

Add the following action to the _construct method in the class-plugin-name-admin.php file.

add_action('init', array( $this, 'register_custom_post_types' ));

STEP 2: Create the Register Custom Post Types Method

public function register_custom_post_types(){
$productArgs = array(
			'label'=>'WPMerchant Products',
					'add_new'=>'Add Product',
					'add_new_item'=>'Add New Product',
					'edit_item'=>'Edit Product',
					'new_item'=>'New Product',
					'view_item'=>'View Product',
					'search_items'=>'Search Product',
					'not_found'=>'No Products Found',
					'not_found_in_trash'=>'No Products Found in Trash'
			'description'=>'WPMerchant Products', 
			'supports'=>array('title','thumbnail', 'custom_fields'),
// Post type, $args - the Post Type string can be MAX 20 characters
register_post_type( 'wpmerchant_products', $productArgs );

STEP 3: Add the following to the construct method

Then, you need to add the admin_menu action from the WordPress Plugin API to your _construct method.

add_action('admin_menu', array( $this, 'addPluginAdminMenu' ), 9);

STEP: 4: Add the addPluginAdminMenu Method

You referenced the addPluginAdminMenu function in the admin_menu action above, so you need to add that function to your class-plugin-name-admin.php file’s _construct method.

public function addPluginAdminMenu() {
//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
add_menu_page( 'WPMerchant', 'WPMerchant', 'administrator', $this->plugin_name, array( $this, 'display_plugin_admin_dashboard' ), plugin_dir_url( FILE ) . 'img/logo2.png', 26 );

Your custom post type will now show up when you hover over your plugin name in the left sidebar of the WordPress Admin Dashboard. If you click on your custom post type name, you will see the list of existing custom post type instances and can add or edit an instance.

In the next post, learn how to add an admin post action to your WordPress plugin, by clicking on the button below:

Add WordPress Admin Post Action

Leave a Reply

Your email address will not be published. Required fields are marked *