- Intro to WordPress Plugin Development
- Intro to WordPress Plugin Development: Best Practices
- Intro to WordPress Plugin Development: Using Filters
- Intro to WordPress Plugin Development: Using Actions
- Intro to WordPress Plugin Development: Register Custom Post Types
- Intro to WordPress Plugin Development: Shortcodes
- Intro to WordPress Plugin Development: Loading Scripts and Styles
- Intro to WordPress Plugin Development: Add a Menu to Dashboard
- Intro to WordPress Plugin Development: Adding a Settings Page
- Intro to WordPress Plugin Development: Sanitize and Validate Data
- Intro to WordPress Plugin Development: Object Oriented Programming
- Intro to WordPress Plugin Development: Separate Into Multiple Files
So far in the Intro to WordPress Plugin Development series, we’ve talked about properly formatting code, and how to use filters and actions. Next, we’ll look at creating our own custom post type.
What are WordPress custom post types?
WordPress comes with several different post types, like posts and pages. Since WordPress started off as a blogging platform, the name “post” sort of stuck, but a custom post type might more accurately be called a custom content type. Regardless of the name, a custom post type refers to different types of content.
Technically, you don’t really need to use a custom post type. You could categorize your content using the built-in categories or tags. However, some content doesn’t need a full-blown text editor like posts have. Some content may require custom fields that shouldn’t be made available to other post types.
Let’s look at some examples of when a custom post type might make sense.
- Content that doesn’t need to be listed in a chronological order like posts are can be made into a custom post type. For example, a list of sports teams doesn’t need to be displayed in chronological order along with your blog post.
- Content that requires additional fields might be made into a custom post type. For example, a “book” custom post type might have custom fields like author, published date, ISBN, rating, etc.
- Content that shouldn’t be visible publically on the site can be made into a custom post type. Custom post types have a setting that enables the content to be publically visible. If you have some information that you only want visible to administrators on your site via the dashboard, you can turn off the public visibility for this content.
Let’s build a custom post type
We’re going to use the example of a sports team custom post type. This can be useful for anything from a town’s Little League website, to a pro-sports news site.
To add our custom post type we’ll use the function register_post_type(). In the WordPress codex, it says that this function should only called with the init
action. If you remember the previous lesson on using actions, you might be able to guess why.
Any call to register_post_type()
before the init action runs will not work. Any call to it after the init action runs may not work correctly. Basically, WordPress is telling us that the init
action is the right place for this function to run.
function team_cpt_new_post_type(){ register_post_type( 'teams', array( 'labels' => array( 'name' => __( 'Teams' ), 'singular_name' => __( 'Team' ) ), 'public' => true, 'has_archive' => true, 'rewrite' => array( 'slug' => 'teams', ), 'supports' => array( 'title', 'editor', ), ) ); } add_action( 'init', 'team_cpt_new_post_type' ); |
What this code does is it registers a post type ‘teams’ with a series of arguments. These arguments tell our custom post type what it can and can’t do.
The first argument is labels
, which defines the words we see on the screen when referring to this post type. In this case, we see “Teams” when referring to several of this post type, and “Team” when referring to one of them.
The second argument is public
, which dictates whether or not this post type is available on the front end of the site.
The third argument is has_archive
, which tells WordPress if this post type should have an archive page, similar to your main blog page for your posts.
The fourth argument is rewrite
. This tells WordPress what to use in the URL when accessing posts of this type. For example, your site might access a “team” post at http://example.com/teams/boston-red-sox
The fifth argument is supports
. This indicates what features we need our custom post type to have. The title and editor arguments should be familiar to you. These enable the post’s title and the main editor field.
There are a lot of other arguments you can use for your post type, but this is the basics.
Add content to our custom post type
Now, adding content to our new custom post type is just as easy as writing a new blog post. The difference is that the content won’t appear on the blog page. It will now appear in our newly created “Teams” page, which would be located at http://example.com/teams/
Leave a Reply