5.6: Registering Custom Post Types

Section 5.6 PHP Registering Custom Post Types

WordPress comes with just one Post type and it is used for writing Blog posts. What if we want to be able to store more than one type of post?

What if your site needs to be able to render data on the fly to the screen based at the whim of a site visitor? Fortunately we have a way of creating custom post types that we can use to meet this need and it's a good thing we do because we need to build Artist, Event, Venue, and Genre custom post types for our site.</b>

Figure 5.6.1

  • Key "register post type" in the search field and then click the magnifying glass button.

Figure 5.6.2

  • The search will return a number of links where information about registering post types can be found. At this time the very first link listed has what we are looking for.

Figure 5.6.3

  • Scroll down and you will see that everything you ever want to know about registering post types can be found at this location. Keep this resource in mind when you find it necessary to create a custom post type for your WordPress site.

  • Custom post types must be registered and defined within a PHP function. Since we are not using a plugin to create our custom post type we are going to code our own using PHP.

  • We may be tempted to code this functionality into the functions.php file within our child theme and this will definitely work but it creates an unnecessary risk for your WordPress site.

  • If we update the theme to give our site a new look, our current functions.php file will be over-written when the new theme is installed.

  • Clearly we do not want to lose the code that registers and defines our custom post types and there is a solution.

  • We can avoid the risk of having our custom post type code over-written by using the "must use plugins" (mu-plugins) folder is a special folder that we will place inside the wp-content folder at the root of the Website.

  • Because the mu-plugins folder is outside of the themes folder its contents will not be overwritten if a new theme is installed.

  • WordPress always runs the code inside the mu-plugins folder upon start-up.

  • Navigate to the wp-content folder for the Diamond Dog Entertainment Website and create the mu-plugins folder if it is not already there.

Figure 5.6.4

  • Let's register and define our first custom post type. We will start by defining a post type that will hold the information about each artist signed to Diamond Dog Entertainment.

  • Create a new file within the mu-plugins folder. You can name this file anything you like, the name is not important as long as the file extension is .php

  • Next we will do the coding necessary to install this new custom post type into our site so that it is available for use.

1. <?php
2. function diamond_post_types() {
3. register_post_type('artist', array(
4. 'supports' => array('title', 'editor', 'excerpt', 'custom-fields','thumbnail'),
5. 'rewrite' => array('slug' => 'artist'),
6. 'has_archive' => true,
7. 'public' => true,
8. 'labels' => array( 'name' => 'Artists',
9. 'add_new_item' => 'Add New Artist',
10. 'edit_item' => 'Edit Artist',
11. 'all_items' => 'All Artists',
12. 'singular_name' => 'Artist'
13. ),
14. 'menu_icon' => 'dashicons-admin-users'
15. ));
16. }
17. add_action('init', 'diamond_post_types');
18. ?>

Figure 5.6.5

  • Let's break the code down in Figure 5.6.4

  1. First we signal to the server PHP code follows with the <?php tag

  2. function diamond_post_types() { We define a new function that will eventually hold all of our custom post type registrations.

  3. We start the registration using the WordPress PHP library function register_post_type('artist', array(

    • The first parameter in this function call is the post type which we are calling 'artist'.

    • The second parameter is an array that has several values.

  4. 'supports' => array('title', 'editor', 'excerpt', 'custom-fields','thumbnail'), makes these three features available.

  5. 'rewrite' => array('slug' => 'artist'), designates the value for the "slug" in the URL.

Slugs are usually a URL friendly version of the post title

  1. 'has_archive' => true, Enables post type archivesPublic =>true makes the this post type visible to editors and viewers of the site.

  2. 'labels' => array( 'name' => 'Artists', this and the lines all the way up to line 13 set up labels for the operations authorized for this custom post type.

  3. 'menu_icon' => 'dashicons-admin-users' identifies the icon that will designate artist post type in the Dashboard navigation window.

  4. )); closes out the registration of the artist post type.

  5. } the closing bracket signals the end of the diamond_post_types() function.

  6. add_action('init', 'diamond_post_types'); is a call to a WordPress PHP library function to run the diamond_post_types function.

  7. ?> the close PHP tag signals the server that we are finished processing php

  • Now we will register all of the custom post types required for the Diamond Dog Entertainment Website. Use the example at figure 5.6.5 to register these Custom Post Types.

    • Artist

    • Event

    • Genre

    • Venue

All of these custom post type registration must be placed inside the

diamond_post_types() function. Place a comment before each registration to document and make this code easier for others to read and maintain</b>

Figure5.6.6

Once you have set up all of the custom post type registrations access the diamonddogsentertainment WordPress dashboard. You should see all of the new post types in the dashboard's left hand navigation menu:

Last updated