Custom Post Types
This document explains how to create and manage Custom Post Types in WP Bones. It provides steps to create a Custom Post Type Service Provider using the php bones make:cpt MyCustomPostType
command and how to manually create one. The new provider will be created in the plugins/CustomPostTypes
directory. The document also covers defining the post type key, name, and plural name, and how to override the boot
method to register custom actions and filters.
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.
<?php
namespace WPKirk\CustomPostTypes;
use WPKirk\WPBones\Foundation\WordPressCustomPostTypeServiceProvider;
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
/**
* Post type key. Must not exceed 20 characters and may only contain
* lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().
*
* `register_post_type( $post_type, $args = array() )`
*
* @var string
*/
protected $id = 'wp_kirk_startship';
/**
* Name of the post type shown in the menu. Usually plural.
*
* @var string
*/
protected $name = 'Starship';
/**
* Name of the post type shown in the menu as plural.
*
* @var string
*/
protected $plural = 'Starships';
/**
* 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
}
}
For more details about the WordPressCustomPostTypeServiceProvider
class, please refer to the WordPressCustomPostTypeServiceProvider documentation.
Load the Custom Post Type Service Provider
Add this new Service Provider to the list of providers in the /config/plugin.php
file:
/*
|--------------------------------------------------------------------------
| Custom Post Types
|--------------------------------------------------------------------------
|
| Here is where you can register the Custom Post Types.
|
*/
'custom_post_types' => [ '\WPKirk\CustomPostTypes\MyCustomPostType' ],
Meta post type
You may register a meta post type by adding the following code to your Service Provider:
/**
* Register post meta
*/
public function registerPostMeta()
{
// You may override this method
return [
'wp_kirk_startship_code_name' => [
'single' => true,
'show_in_rest' => true,
'type' => 'string',
]
];
}
Meta boxes
You may register a meta box by adding the following code to your Service Provider:
/**
* This action is called when you can add the meta box
*/
public function registerMetaBoxes()
{
return [
[
'id' => 'wp_kirk_startship_code_name',
'title' => __('Starship Code', 'wp-kirk'),
'view' => [$this, 'metaBoxStartshipCodeView'],
'context' => 'normal',
'priority' => 'high',
'callback_args' => null
]
];
}
/**
* This method is called by the meta box
*/
public function metaBoxStartshipCodeView($post)
{
echo WPKirk()->view('cpt.code')->with('starship', $post);
}
Meta box props
Every meta box has the following props:
Prop | Type | Required | Description |
---|---|---|---|
id | string | Yes | Meta box ID (used in the ‘id’ attribute for the meta box). |
title | string | Yes | Title of the meta box. |
view | callable | Yes | The view to render the meta box. |
context | string | No | The context within the screen where the boxes should display. Available contexts vary from screen to screen. Post edit screen contexts include ‘normal’, ‘side’, and ‘advanced’. Default is ‘advanced’. |
priority | string | No | The priority within the context where the boxes should show (‘high’, ‘low’). Default ‘default’. |
callback_args | mixed | No | Data that should be set as the $args property of the box array (which is the second parameter passed to your callback). Default null. |
Columns
You may add columns to your post type by adding the following code to your Service Provider:
/**
* Register columns
*/
public function registerColumns()
{
return [
[
'id' => 'wp_kirk_startship_code_name',
'title' => __('Starship Code', 'wp-kirk'),
]
];
}
You may also add a custom column content by adding the following code to your Service Provider:
/**
* This action is called when you can add the column content
*/
public function columnContent($column_id, $value, $post)
{
if ($column_id === 'wp_kirk_startship_code_name') {
echo "[{$value}]";
}
}
This can be useful if you want to display a custom column content as date, image, etc.
Placeholder Title
You may add a placeholder title to your post type by adding the following code to your Service Provider:
/**
* Register placeholder title
*/
public function registerPlaceholderTitle()
{
return __('Enter the starship name here', 'wp-kirk');
}
Add MetaBox after the title
You may add a meta box after the title by adding the following code to your Service Provider:
/**
* Register meta box after the title
*/
public function registerAfterTitleView()
{
global $post;
echo WPKirk()->view('cpt.after-title')->with('starship', $post);
}
<?php
// in $startship you will have the post object
if (!defined('ABSPATH')) exit;
?>
<h3>
<?php _e('Any view after the title', 'wp-kirk'); ?>
<?php echo $starship->ID ?>
</h3>
Add submenu and view to the post type
You may add a submenu to the custom post type menu directly in the config/menu.php
file, by using 'edit.php?post_type={cpt_id}'
menu key:
/*
|--------------------------------------------------------------------------
| Menu
|--------------------------------------------------------------------------
|
| Here is where you can register the menu.
|
*/
return [
'wp_kirk_slug_menu' => [
"page_title" => "WP Kirk Page",
"menu_title" => "WP Kirk Menu",
'capability' => 'read',
'icon' => 'wpbones-logo-menu.png',
'items' => [
[
"page_title" => "Main View",
"menu_title" => "Main View",
'capability' => 'read',
'route' => [
'get' => 'Dashboard\DashboardController@index'
],
],
]
],
'edit.php?post_type=wp_kirk_startship' => [
"menu_title" => "Custom Post Type Settings",
'capability' => 'read',
'items' => [
'overview' => [
"menu_title" => __("Settings", 'wp-kirk'),
'capability' => 'read',
'route' => [
'get' => 'Dashboard\SettingsController@index'
],
],
]
]
];
You will be able to use it as WP Bones submenu, following the same structure as the main menu.