Learn how to create a plugin that registers a custom post type

Learn how to create a plugin that registers a custom post type

Chris Miller's photo
Chris Miller

Published on Jul 31, 2021

4 min read

Subscribe to my newsletter and never miss my upcoming articles

In the previous article — we walked through creating a plugin by only adding a folder, a file, and the plugin header information at the top.

This article will create functionality for our plugin to help demonstrate how you can build your own WordPress plugin.

If you've been following along — welcome back!

The scope

Let's keep this small and simple in scope by writing some functionality that will display a message at the bottom of the page — appending it to the footer.

Planning the plugin

Understanding that the scope is to display a message at the bottom of the page, we will need to find a WordPress hook that will allow us to do that.

Luckily, WordPress has an action hook for just this called "wp_footer."

If you're not familiar with hooks — they allow you to do things at a particular place and time during the loading sequence of the CMS.

I'll expand on hooks in a future post.

Let's build!

If we take a look at the wp_footer documentation it will tell us how to use this hook along with a few great examples.

First, let's create our function.

<?php

/**
 * Plugin Name: Pulp Fiction Quote
 */

function display_message() {
  echo '<div>What does Marcellus Wallace look like?</div>';
}

Now that we've created our function let's add it to the wp_footer action hook.

<?php

/**
 * Plugin Name: Pulp Fiction Quote
 */

function display_message() {
  echo '<div>What does Marcellus Wallace look like?</div>';
}
add_action('wp_footer', 'display_message');

Boom — that's it! If you activate the plugin and visit any page on the site, you will see our message appear at the bottom.

Creating more functionality

If you're already familiar with building a WordPress theme, then you'll know that the functions.php file is a pretty important file when you need to do things like register custom post types, use hooks, etc.

Guess what? You can paste all of that right into your plugin, and it will work all the same!

You could be sitting on a ton of useful code that you've already written and easily turn it into a plugin — crazy, I know.

Let's try creating a custom post type — which is a fairly common thing to do with any custom WordPress build.

Create a custom post type

Okie dokes — let's start by wiping the slate clean and renaming our plugin to "Movies Post Type".

<?php

/**
 * Plugin Name: Movies Post Type
 */

Let's drop in all of the code we would need to register a custom post type.

Note: If you're not familiar with registering custom post types, I will cover this in a separate article.

/**
 * Plugin Name: Movies Post Type
 */

// Register Custom Post Type
function custom_post_type() {

  $labels = array(
    'name'                  => _x('Movies', 'Post Type General Name', 'text_domain'),
    'singular_name'         => _x('Movie', 'Post Type Singular Name', 'text_domain'),
    'menu_name'             => __('Movies', 'text_domain'),
    'name_admin_bar'        => __('Movie', 'text_domain'),
    'archives'              => __('Item Archives', 'text_domain'),
    'attributes'            => __('Item Attributes', 'text_domain'),
    'parent_item_colon'     => __('Parent Item:', 'text_domain'),
    'all_items'             => __('All Items', 'text_domain'),
    'add_new_item'          => __('Add New Item', 'text_domain'),
    'add_new'               => __('Add New', 'text_domain'),
    'new_item'              => __('New Item', 'text_domain'),
    'edit_item'             => __('Edit Item', 'text_domain'),
    'update_item'           => __('Update Item', 'text_domain'),
    'view_item'             => __('View Item', 'text_domain'),
    'view_items'            => __('View Items', 'text_domain'),
    'search_items'          => __('Search Item', 'text_domain'),
    'not_found'             => __('Not found', 'text_domain'),
    'not_found_in_trash'    => __('Not found in Trash', 'text_domain'),
    'featured_image'        => __('Featured Image', 'text_domain'),
    'set_featured_image'    => __('Set featured image', 'text_domain'),
    'remove_featured_image' => __('Remove featured image', 'text_domain'),
    'use_featured_image'    => __('Use as featured image', 'text_domain'),
    'insert_into_item'      => __('Insert into item', 'text_domain'),
    'uploaded_to_this_item' => __('Uploaded to this item', 'text_domain'),
    'items_list'            => __('Items list', 'text_domain'),
    'items_list_navigation' => __('Items list navigation', 'text_domain'),
    'filter_items_list'     => __('Filter items list', 'text_domain'),
  );
  $args = array(
    'label'                 => __('Movie', 'text_domain'),
    'description'           => __('Movie Description', 'text_domain'),
    'labels'                => $labels,
    'supports'              => false,
    'taxonomies'            => array('category', 'post_tag'),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
  );
  register_post_type('movie', $args);
}
add_action('init', 'custom_post_type', 0);

If you go back to your plugins in the backend of WordPress and activate this plugin — you should now see a "Movies" menu appear in the backend of WordPress, similar to "Pages", that will allow you to begin adding movies!

giphy (6).gif

Wrapping up

I hope you take away from this article that creating a WordPress plugin is not a difficult thing to do — once you understand why and how.

Continue to the next article and learn how to use a WordPress plugin boilerplate generator to create a well-structured, object-oriented framework to begin building production-ready plugins.


Keep reading

This is a series that will teach you how to build a plugin from scratch.

We will get out hands dirty, and by the end of this series, you should be able to take an idea that you have and turn it into a plugin yourself!

Scroll down to find the other articles in this series.

Did you find this article valuable?

Support Chris Miller by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this
Proudly part of