Custom Post Types

Overview

WordPress provides a powerful way to create your own content types. WP Bones provides a simple way to add your own Custom Post Types.

Create a Custom Post Type Service Provider

You may create your own Custom Post Type Service Provider by following the steps below:

php bones make:cpt MyCustomPostType

By default, the new provider will be created in the plugins/CustomPostTypes directory. Of course, you may create your Service Provider manually and in any directory you prefer. You have to change the namespace accordingly.

/plugin/CustomPostTypes/MyCustomPostType.php
<?php
namespace WPKirk\CustomPostTypes;
 
use WPKirk\WPBones\Foundation\WordPressCustomPostTypeServiceProvider;
 
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
 
  protected $id     = 'wp_kirk_cpt';
  protected $name   = 'Captain';
  protected $plural = 'Captains';
 
  /**
   * You may override this method in order to register your own actions and filters.
   *
   */
  public function boot()
  {
    // You may override this method
  }
 
  /**
   * Override this method to save/update your custom data.
   * This method is called by hook action save_post_{post_type}`
   *
   * @param int|string $post_id Post ID
   * @param object     $post    Optional. Post object
   *
   */
  public function update( $post_id, $post )
  {
    // You can override this method to save your own data
  }
 
}

Load the Custom Post Type Service Provider

Add this new Service Provider to the list of providers in the /config/plugin.php file:

config/plugin.php
  /*
  |--------------------------------------------------------------------------
  | Custom Post Types
  |--------------------------------------------------------------------------
  |
  | Here is where you can register the Custom Post Types.
  |
  */
 
  'custom_post_types' => [ '\WPKirk\CustomPostTypes\MyCustomPostType' ],