How to Create a Basic WordPress Plugin With a Custom Post Type In A Few Simple Steps

No Comments

The plugin community for WordPress is largely responsible for the CMS’s popularity today. With plugins, there are literally almost no limits to what can be accomplished using this simple blogging platform. Plugins provide the additional functionality that allows you to customise your site to perform exactly how you would like it to. WordPress plugins are known for there ease of use, their open source architecture, and their strong community support but they don’t always come without caveats. Sometimes the simple functionality that comes with a plugin has a cost that is out of our budget range, or the display just isn’t right, or even missing that 1 simple feature you absolutely need. There are not many options available for customizing the plugin to your exact preferences. Sometimes the functionality simply doesn’t exist. This article covers the basic steps to creating a simple plugin that allows you to hook into the WordPress functionality and easily utilize your desired functionality. You can go directly to the files here on GitHub or view the text version below.

Basic Plugin With Custom Post Type

 

Let’s start with the basic layout for a basic plugin that adds a new custom post type. You will have to locate your WordPress plugin directory and create a couple of files to get started. We prefer to vendor prefix our files in order to avoid conflicts with other plugins. For instance, this plugin will be for an item sale type post and for semantics the plugin name will be salepost and we will use rapidmod for our vendor. That means we will use the <VENDOR>_<PLUGIN_NAME> structure and the name for our new plugin directory will be rapidmod_salepost. Inside of that directory, you need 1 file, rapidmod_salepost.php. The PHP file should contain the functionality required to initialize its actions.

Directory Structure:

 

<PLUGIN_DIR>
<PLUGIN_DIR>/<VENDOR>_<PLUGIN_NAME>
<PLUGIN_DIR>/<VENDOR>_<PLUGIN_NAME>/<VENDOR>_<PLUGIN_NAME>.php

 

Or in our specific case:

plugins/
plugins/rapidmod_salepost
plugins/rapidmod_salepost/rapidmod_salepost.php

 

Now its time to add some code to our plugin PHP file and make that custom post type work. Open rapidmod_salepost.php and include the opening PHP tags. A comment block is required at the beginning of the file to define the plugin itself. The only required parameter is the “Plugin Name:” This is the name that will display in the list of plugins inside of the plugins menu section. As you can see we added some additional parameters which can be found here in the WordPress header requirements documentation.

 

Our Comment Block Parameters
/**
  * Plugin Name: Sale Post
  * Plugin URI: https://rapidmod.com/wordpress/how-to-create-a-wordpress-plugin-in-a-few-simple-steps
  * Description: Creates a custom sale type post.
  * Version: 0.1
  * Author: Rapidmod.com
  * Author URI: http://rapidmod.com/
  * License: GPLv2
  * License URI: https://www.gnu.org/licenses/gpl-2.0.html
***/

 

Now it is time to create an init (initialization) function that registers our new post type. We will use the same semantic structure as above for our function naming process. since this is an initialization function we will prefix the function with init and use the function name init_rapidmod_salepost, this function will initialize our plugin when WordPress is loaded. Our goal is to create a custom post type inside of WordPress and give it the same basic functionality as a WordPress post. To do this we will need to create a function inside of our PHP file and register a new post type using the same semantic structure, this time we are adding two variables <LABEL> Which we will use to display a singular term and the <PLURAL> variable to define the term used for collections or lists. After our function is defined we can add it to WordPress’s init action or hook.
 

Basic Plugin Custom Post Template
<LABEL> = Sale Item
<PLURAL> = Sale Items

function init_<VENDOR>_<PLUGIN_NAME>() {
   register_post_type( '<VENDOR>_<PLUGIN_NAME>',
      array(
         'labels' => array(
            'name' => '<LABEL>',
            'singular_name' => '<LABEL>',
            'add_new' => 'Add New <Label>',
            'add_new_item' => 'Add New <LABEL>',
            'edit' => 'Edit <LABEL>',
            'edit_item' => 'Edit <LABEL>',
            'new_item' => 'New <LABEL>',
            'view' => 'View <LABEL>',
            'view_item' => 'View <LABEL>',
            'search_items' => 'Search <PLURAL>',
            'not_found' => 'No <PLURAL> found',
            'not_found_in_trash' => 'No <PLURAL> found in Trash',
            'parent' => 'Parent <LABEL>'
         ),

         'public' => true,
         'menu_position' => 15,
         'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
         'taxonomies' => array( '' ),
         'has_archive' => true
      )
   );
}
add_action( 'init', 'init_<VENDOR>_<PLUGIN_NAME>' );

 

If you made it this far I am hoping the labels parameter is mostly self-explanatory. If you need more help again WordPress has a more in-depth look at the post type parameters and what each one does. This platform has come along way since it’s inception and has evolved into an easily customizable cms basic platform. The template will create a new post type available in the backend of WordPress to edit that has the same basic functionality as a regular post without tags or categories. It is, however, important to note some things about the arguments passed into the  register_post_type function. In theory setting the “public” parameter to true the defaults should kick in which includes the admin UI and allowing the post to be returned in the search results on the front end. I have read that is not always the case and instead of relying on the default answer which is more process intensive anyway set both “show_ui” and “publicly_queryable” to true as well.

"publicly_queryable" => true,
"show_ui" => true,

Now that the parameters are set we are ready to add the function to the WordPress init action. The init action processes as WordPress is loading its application. By using the register_post_type function we are creating an object and saving it to the global WordPress variable $wp_post_types, or more specifically in our case $wp_post_types[<VENDOR>_<PLUGIN_NAME>], and setting it as a new instance of the class WP_Post_Type. The new WP_Post_Type instance also contains the registered meta boxes, URL rewriting rules and what the post type supports. After the $wp_post_type variable is set the register_post_type function goes on to add additional hooks from the arguments, register the taxonomies and finally does the action of register_post_type. Yes, you are correct! The only creating we just did was setting values to parameters and variables. That is all WordPress needs for the new post functionality a set of variables or arguments.

 

Here is Our Completed PHP File
 plugins/rapidmod_salepost/rapidmod_sale_post.php
<?php
/**
  * Plugin Name: Sale Post
  * Plugin URI: https://rapidmod.com/wordpress/how-to-create-a-wordpress-plugin-in-a-few-simple-steps
  * Description: Creates a custom sale type post.
  * Version: 0.1
  * Author: Rapidmod.com
  * Author URI: http://rapidmod.com/
  * License: GPLv2
  * License URI
***/


function init_rapidmod_salepost() {
   register_post_type( 'rapidmod_salepost',
      array(
         'labels' => array(
            'name' => 'Item',
            'singular_name' => 'Sale Item',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Sale Item',
            'edit' => 'Edit',
            'edit_item' => 'Edit Sale Item',
            'new_item' => 'New Sale Item',
            'view' => 'View',
            'view_item' => 'View Sale Item',
            'search_items' => 'Search Sale Items',
            'not_found' => 'No Sale Items found',
            'not_found_in_trash' => 'No Sale Items found in Trash',
            'parent' => 'Parent Sale Item'
         ),

         'public' => true,
         "publicly_queryable" => true,
         "show_ui" => true,
         'menu_position' => 15,
         'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
         'has_archive' => true
      )
   );
}
add_action( 'init', 'init_rapidmod_salepost' );
?>

 
 

Activate The Plugin (Screenshots):

 
Now its time to navigate to the plugins page. Our new plugin should display in the list of plugins as Sale Post. After Activating the plugin a new menu item should display in the admin bar, the menu item should be Item. As you can see by the screenshots below this simple functionality is in place we have created a whole new post type inside of WordPress that has the same basic functionality of a regular WordPress page post type. If you would like more information or have a question please use the comments to let me know. Or you could read up on WordPress Plugins here.

Activate the plugin

Activate

Plugin Activated

Create a new item

 

 

 

 

 

 

 

 

 

Leave a Reply

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