Learn WordPress Custom Post Types

Custom Post Types

1. Create a Folder in Plugins

For example my-great-plugin.

2. Create the Main PHP File

In this case which should have the same name as the plugin folder, ex: my-great-plugin.php

Add plugin header, e.g.:

[php]
/**
* Plugin Name: My Great Plugin
* Plugin URI: http://www.mygreatplugin.com/isnt/really/here/
* Version: 0.1
* Author: My Great Name
* Author URI: http://www.davemackey.net/
* Description: It's REALLY awesome!
* License: GPL2
*/
[/php]

Add a Custom Class:
(This avoids function naming conflicts with WP and other plugins)

[php]
class SermonManagerPlus {
     // code will go here
}
[/php]

Add a Constructor:

[php]
       /**
       * Constructor: Called when plugin is initialized.
       */
       function __construct() {
        add_action( 'init', array( $this, 'register_custom_post_type' ) );
       }
[/php]

Register the Custom Post Type

Create a function that will hold the code to actually register the post type:

[php]
function register_custom_post_type() {

}
[/php]

Utilize WordPress built-in function register_post_type() within register_custom_post_type() function:

[php]
   register_post_type( 'contact', array(
          'labels' => array(
              'name' => _x( 'Series', 'post type general name', 'sermon-manager-plus' ), // Plural name of CPT
              'singular_name' => _x( 'Series', 'post type singular name', 'sermon-manager-plus' ), // Singular name of CPT
              'menu_name' => _x( 'Sermons', 'admin menu', 'sermon-manager-plus' ), // Appears on the admin menu.
              'name_admin_bar' => _x( 'Series', 'sermon-manager-plus'),  // Appears on top fixed admin menu under new.
              'add_new' => _x( 'Add New', 'series', 'sermon-manager-plus'), // Add in admin menu under menu_name.
              'add_new_item' => __( 'Add New Series', 'sermon-manager-plus'),
              'new_item' => __( 'New Series', 'sermon-manager-plus'),
              'edit_item' => __( 'Edit Series', 'sermon-manager-plus'),
              'view_item' => __( 'View Series', 'sermon-manager-plus'),
              'all_items' => __( 'All Series', 'sermon-manager-plus'),
              'search_items' => __( 'Search Series', 'sermon-manager-plus'),
              'parent_item_colon' => __( 'Parent Series:', 'sermon-manager-plus'), // Only used with hierarchical CPT.
              'not_found' => __( 'No Series Found.', 'sermon-manager-plus'),
              'not_found_in_trash' => __('No Series Found in Trash.', 'sermon-manager-plus'),
              ),
              
              // Frontend
              'has_archive' => false, // Specific archive template?
              'public'  => false, // Can be seen by public?
              'public_queryable' => false, // Queryable by public?
              
              // Admin
              'capability_type' => 'post', // Use permissions as those used for post.
              'menu_icon' => 'dashicons-info', // See developer.wordpress.org/resource/dashicons/
              'menu_position' => 10,  // Appears below this menu item at the given value: 5 (posts), 10 (media), 15 (links), 20 (pages), 25 (comments)
              // 60 (first separator), 65 (plugins), 70 (users), 75 (tools), 80 (settings), 100 (second separator)
              'query_var' => true, // Query variable is set to post type.
              'show_in_menu' => true, // can be false (don't show), true (show top-level), or a string like 'tools.php' or 'edit.php?post_type="page"', e.g. if you want
              // to nest this CPT under another CPT, for example Sermons nested under Series.
              'show_ui' => true, // Generates a default UI for managing post type in admin.
              'supports' => array(
                  'title',
                  'author',
                  'comments'
                  ), // Options: title, editor, author, thumbnail, excerpt, trackbacks, custom-fields, comments, revisions, page-attributes, post-formats
                  ) );
[/php]
  • set_post_format()
  • get_post_format()  – Check format for post, change its presentation.
  • has_post_format() – Check if post is of specific format.
  • get_post_format_link()
  • get_post_format_string()
  • add_theme_support() – Must be used to enable specific post formats.
    • ex.: add_theme_support( ‘post-formats’, array( ‘aside’, ‘gallery’ ) );
    • Must be called before init hook, recommended hook: after_setup_theme
  • The above enables support, but then which types of posts support it? Pages? Posts? Use add_post_type_support() to enable:
    • ex. add_post_type_support( ‘page’, ‘post-formats’ );
      add_post_type_support( ‘my_custom_post_type’, ‘post-formats’ );
    • Alternatively this can be enabled in register_post_type()
  • Child themes inherit parent’s theme add_theme_support(), but can also override it (but does not add, so include the parent’s in add_theme_support()).
  • remove_theme_support(‘post-formats’) – Removes post format support entirely.

Bibliography / Further Reading