Documentation
Services Provider
Custom Post Types

Custom Post Types

You can add your Custom Post Type (CPT) by editing the /config/plugin.php file. This file is very important because it returns several important information used while the plugin is started. Let see a typical structure of this file:

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

Typical structure of a Custom Post Type Service Provider

Let's see a typical class

/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
  }
 
}

PHP Bones Command

Using the php bones make:cpt Bones command, we can quickly create a Custom Post Type Service Provider.

php bones make:cpt MyCustomPostType