How to Create a Custom Post Type in WordPress with a Simple Plugin
TL;DR Summary: Create a custom post type plugin in WordPress to organize and display content uniquely. Understand the benefits of custom post types and how to develop a plugin for them. Learn step-by-step how to register a custom post type for "Books." Enhance your site's functionality and user experience with this guide. Read more for details on the same page.
Full Plugin Code Example Included Below
Key Takeaways
- Understand the basics of WordPress plugin development.
- Learn how to create a custom post type.
- Discover how to organize content using custom post types.
- Gain insights into enhancing your site’s functionality.
- Explore the potential of custom post types for content presentation.
Developing a WordPress plugin is an effective way to extend the functionality of your WordPress site. One of the powerful features you can add is a custom post type, allowing you to organize and display content in a way that suits your needs.
By the end of this article, you’ll understand how to create a custom post type in WordPress by developing a simple plugin. This will enable you to tailor your site’s content presentation to your specific requirements.
Examples of Custom Post Types
For example, if you want to add a new type of content to your WordPress siteโlike โRecipes,โ โPortfolio,โ or โBooksโโyou donโt need a plugin from the directory. You can *create your own* with just a few lines of code using a custom plugin. This guide shows you how to create a simple custom post type plugin for WordPress.
Understanding Custom Post Types in WordPress
Understanding custom post types is crucial for any WordPress user looking to enhance their website’s functionality and user experience. Custom post types are a powerful feature in WordPress that allows users to create and manage content beyond the standard posts and pages.
What Are Custom Post Types?
Custom post types are a way to categorize and display content in a manner that suits the site’s specific needs. They enable developers and site owners to create custom content types, such as portfolios, testimonials, or news articles, which can be managed independently of the default posts and pages. This flexibility is a key aspect of WordPress customization, allowing for a more tailored content management system.
Why Use Custom Post Types?
The use of custom post types offers several benefits. It allows for a more organized content structure, making it easier for users to find and manage the content they need. Custom post types also enable the creation of custom templates and display formats, enhancing the overall user experience. By utilizing custom post types, developers can create a more robust and flexible content management system.
Default Post Types in WordPress
Before diving into custom post types, it’s essential to understand the default post types available in WordPress. These include posts, pages, attachments, revisions, and navigation menus. While these default types cover basic content management needs, custom post types provide the flexibility to go beyond these limitations, enabling the creation of bespoke content types that suit specific requirements.
By understanding and leveraging custom post types, WordPress users can significantly enhance their site’s functionality and provide a better experience for their visitors.
Prerequisites for Creating a WordPress Plugin
Before diving into WordPress plugin development, it’s essential to understand the prerequisites that will help you create a successful plugin. Developing a plugin requires a combination of the right tools, knowledge, and environment.
Required Tools and Knowledge
To start, you’ll need a code editor or IDE (Integrated Development Environment) that you’re comfortable with. Popular choices include Visual Studio Code, Sublime Text, and PHPStorm. Familiarity with PHP, the programming language used by WordPress, is crucial. You should also have a basic understanding of HTML, CSS, and JavaScript.
Additionally, knowledge of WordPress core functions, hooks, and APIs is necessary for developing a plugin that integrates well with WordPress. Understanding how to use the WordPress Codex and other developer resources will also be beneficial.
Setting Up a Local Development Environment
A local development environment allows you to test and develop your plugin without affecting a live site. Tools like XAMPP, MAMP, or Local by Flywheel can help you set up a local WordPress installation. This environment should mimic your target WordPress version to ensure compatibility.
Understanding Plugin Structure
A WordPress plugin typically consists of a main PHP file and possibly additional files and directories for assets, includes, or other functionality. Understanding how to structure your plugin correctly is vital for maintainability and scalability. The main PHP file should contain the plugin header information, which includes the plugin name, description, version, and author details.
Why Use a Plugin Instead of Adding Code to functions.php?
Adding custom post types directly to your theme’s `functions.php` file worksโbut it ties your content to the theme. If you ever switch themes, that content disappears. A plugin keeps it portable and separate from design choices. As the saying goes: *โThemes are for design. Plugins are for functionality.โ*
How to Create a Custom Post Type Plugin (Step-by-Step)

Letโs walk through how to create a simple plugin to register a custom post type called โBooks.โ
Step 1: Create the Plugin File
Open your WordPress site’s `wp-content/plugins/` folder and create a new folder called `books-post-type`. Inside that folder, create a file called `books-post-type.php`.
Step 2: Add the Plugin Header
At the top of `books-post-type.php`, paste this:
<?php
/*
Plugin Name: Books Post Type
Description: Adds a "Books" custom post type.
Version: 1.0
Author: Your Name
*/Step 3: Register the Custom Post Type
Add this code below the plugin header (same text file):
function register_books_post_type() {
register_post_type('book', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-book',
'supports' => array('title', 'editor', 'thumbnail'),
'show_in_rest' => true, // Enables Gutenberg editor
));
}
add_action('init', 'register_books_post_type');Thatโs it. You now have a new content type called “Books” that shows up in your WordPress dashboard.
How to Activate the Plugin
1. Zip the folder or upload it directly to `wp-content/plugins/`. 2. Go to your WordPress dashboard โ Plugins. 3. Find โBooks Post Typeโ and click **Activate**.
Next Steps (Optional)
- Create custom archive and single templates using
archive-book.phpandsingle-book.php. - Add taxonomies like genres or authors using
register_taxonomy(). - Use WP_Query to show books on your homepage or in widgets.
Conclusion
Creating a custom post type via a plugin is a clean and portable way to expand your WordPress site. Once youโve done this once, you can reuse and adapt this pattern for nearly any kind of content.
By now, you should have a solid understanding of how to create a custom post in WordPress using a simple plugin. This process not only enhances your website’s functionality but also provides a more tailored experience for your users.
Developing a WordPress plugin to register a custom post type offers numerous benefits, including separation of functionality from theme and better organization of content. With the knowledge gained from this article, you can now create a custom post type that suits your needs, whether it’s for news, events, or other types of content.
To further enhance your WordPress skills, consider exploring other plugin development techniques and custom post type configurations. This will enable you to create a more robust and user-friendly website, ultimately improving your online presence.
FAQs About Custom Post Types in WordPress
What is a custom post type in WordPress?
A custom post type is a way to create a new content type in WordPress, allowing users to organize and display content in a unique way, beyond the default posts and pages.
Why should I create a custom post type via a plugin?
Creating a custom post type via a plugin allows for separation of functionality from the theme, enhances portability and reusability, and improves content organization, making it a flexible and maintainable solution.
What are the prerequisites for creating a WordPress plugin?
To create a WordPress plugin, you need to have the required tools, knowledge, a local development environment set up, and an understanding of the basic structure of a plugin.
How do I register a custom post type in WordPress?
You can register a custom post type using the register_post_type() function, configuring post type arguments, and setting up labels, such as singular and plural names, and menu names.
How do I display custom post types on the front end?
To display custom post types on the front end, you can use WP_Query to display posts, create custom templates, add archive and single post support, and customize the post display.
Can I use a custom post type plugin with any WordPress theme?
Yes, a custom post type plugin is designed to be theme-agnostic, allowing you to use it with any WordPress theme, and ensuring that your custom post type is always available, regardless of the theme you are using.
How do I install and activate a custom post type plugin?
To install and activate a custom post type plugin, you need to upload the plugin to your WordPress site, activate it through the WordPress dashboard, and configure any necessary settings.
What is the benefit of using a plugin to create a custom post type?
Using a plugin to create a custom post type provides a flexible and maintainable solution, allowing you to easily add or remove functionality, and ensuring that your custom post type is always available, regardless of the theme you are using.
๐ Download a PDF of This Article

