Today we will be developing a plugin that creates a custom post type in WordPress for us to store data.  In order to do so, we will need to create a plugin folder and make a plugin php template file with certain WordPress hooks. This tutorial assumes that you are running WordPress on a linux box.

Table of Contents:

  1. Create plugin folder and plugin file
  2. Create plugin meta information
  3. Register post type
  4. Disable rich text (optional)
  5. Enqueue scripts & styles (optional)
  6. Register activation hook

1. Create plugin folder and plugin file

To begin creating the plugin we need to create a folder for the plugin code to live.  If you use command line you can type:

cd /{path/to/plugin/directory}
sudo mkdir {plugin_name}
cd {plugin_name}

Make sure to replace the bracketed information with your own values.

2. Create plugin meta information

Now that the plugin directory has been created, it’s time to create the file that creates the actual plugin.  Type the following to create the file and to start editing at the same time:

sudo vim {plugin_name}.php

Where {plugin name} is the name of your plugin.  To start editing using vim, press “A” and then copy and paste the following:

* Plugin Name: {your plugin name}
* Description: {your plugin description}
* Version: 1.0.0
* Author: {your name}
* License: GPL2

The following lines are essential to getting the plugin registered and available in WordPress.

3. Register post type

Now we will need to create a function that defines an array for labels, an array for arguments, and the WordPress hook to register the post type:

function {plugin_name}_cpt() {
  $labels = array(
    'name' => '{plugin name}',
    'singular_name' => '{plugin name}',
    'menu_name' => '{plugin name}',
    'name_admin_bar' => '{plugin name}',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New',
    'new_item' => 'New Item',
    'edit_item' => 'Edit Item',
    'view_item' => 'View Item',
    'all_items' => 'All Items',
    'search_items' => 'Search Item',
    'parent_item_colon' => 'Parent Item',
    'not_found' => 'No items found.',
    'not_found_in_trash' => 'No items found in Trash.',

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_nav_menus' => true,
    'show_in_menu' => true,
    'menu_position' => 101,
    'menu_icon' => 'dashicons-groups',
    'query_var' => true,
    'rewrite' => array( 'slug' => '{plugin slug}' ),
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'posts_per_page' => -1,
    'supports' => array( 'title', 'editor' )
  register_post_type( '{plugin_name}', $args);
add_action('init', '{plugin_name}_cpt' );

Where all the bracketed information needs to be replaced with your own values.  This function will create a labels array as well as an arguments array to assign values to the custom post type sections.  At the end, we register the post type and also add the action to the init hook.

4. Disable rich text (optional)

The next function is optional, but if you would like to remove the rich text option on the default text areas when creating post type content, you can copy and paste:

function {post_type}_disable_rich_text( $default ) {
  if( get_post_type() === '{post_type}_snippets' ) {
    return false;
  } else {
    return $default;
add_filter('user_can_richedit', '{post_type}_disable_rich_text');

This will check the post type and if matched then remove the rich edit format.

5. Enqueue scripts & styles (optional)

Another optional idea is to enqueue scripts and styles in your front-end and back-end.  These functions will enqueue scripts and styles:

function {post_type}_snippets_enqueue_scripts() {
  wp_enqueue_script('{script_name}','{script_url}', array('dependencies'), '{version}', {in_footer});
add_action('wp_enqueue_scripts', '{post_type}_snippets_enqueue_scripts'); 
function {post_type}_snippets_enqueue_styles() {
  wp_enqueue_style('{stylesheet_name}','{style_url}', array('dependencies'), '{version}', {media});
add_action('wp_enqueue_scripts', '{post_type}_snippets_enqueue_styles');

6. Register activation hook

Lastly, we can flush the rewrite on activation and register the full plugin:

function {post_type}_snippets_flush() {
register_activation_hook( __FILE__, '{post_type}_snippets_flush' );

Hopefully this has shown you how it’s possible to create a very basic custom post type plugin.

Leave a Reply

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