Skip to content

Post Types

Miguel Muscat edited this page Nov 30, 2022 · 1 revision

Post types refer to the different classifications that WordPress posts can have. The SDK provides a minimal API that makes it easier to register custom post types in WordPress using modules.

Usage

The PostType class takes two arguments: the slug of the post type and an array of options. Instances can then be registered using the register() method.

use RebelCode\WpSdk\Wp\PostType;

$postType = new PostType('my_type', [
    // Post type arguments
]);

$postType->register();

Refer to the documentation on the register_post_type() function for a list of available arguments.

Helper methods

The PostType class provides a few methods that make it easier to set up post types with common use cases. Note that all of these methods create a new post type instance, and leave the original instance unchanged.

Auto-generated labels

Post types require a lot of labels to be defined. The SDK provides a utility method that can generate these labels using the singular and plural name of the post type.

use RebelCode\WpSdk\Wp\PostType;

$bookType = new PostType('book', [/* ... */]);
$bookType = $bookType->withAutoLabels('Book', 'Books', 'my_text_domain');

No UI

This helper removes all UI aspects of a post type. This is particularly useful for post types that are only used by the plugin as a data-storage entity.

use RebelCode\WpSdk\Wp\PostType;

$bookType = new PostType('book', [/* ... */]);
$bookType = $bookType->withNoUi();

Admin UI only

This helper is similar to the withNoUi() helper method, but it only removes the front-end UI aspects of the post type. You can pass arguments to configure what aspects of the admin UI should be visible. By default, they are all enabled.

use RebelCode\WpSdk\Wp\PostType;

$bookType = new PostType('book', [/* ... */]);
$bookType = $bookType->withAdminUiOnly($adminMenu = true, $adminBar = true, $navMenu = false);

Rest API

This helper configures the post type's REST API endpoints. You can pass arguments to configure it:

use RebelCode\WpSdk\Wp\PostType;

$bookType = new PostType('book', [/* ... */]);
$bookType = $bookType->withRestApi(
    $base = 'books',
    $ns = 'my_plugin/v1',
    $controllerClass = 'My_Books_Controller'
);

Services

Factories for post types can be easily created using the PostType::factory() static method.

Post types can be automatically registered to WordPress by extending the wp/post_types services. The post types in this array service are registered to WordPress by the WordPressModule.

use RebelCode\WpSdk\Wp\PostType;
use RebelCode\WpSdk\Module;
use Dhii\Services\Extensions\ArrayExtension;

class MyModule extends Module
{
    public function getFactories(): array
    {
        return [
            'my_post_type' => PostType::factory('my_post_type', [
                /* ... */
            ]),
        ];
    }
    
    public function getExtensions(): array
    {
        return [
            'wp/post_types' => new ArrayExtension(['my_post_type']),
        ];
    }
}

However, if you wish to use generated labels, you will need to use a regular factory service instead.

use RebelCode\WpSdk\Wp\PostType;
use RebelCode\WpSdk\Module;
use Dhii\Services\Factory;
use Dhii\Services\Extensions\ArrayExtension;

class MyModule extends Module
{
    public function getFactories(): array
    {
        return [
            'my_post_type' => new Factory([], function() {
                $postType = new PostType('my_post_type', [/* ... */]);
                $postType = $postType->withAutoLabels('Book', 'Books', 'my_text_domain');
                
                return $postType
            }),
        ];
    }

    public function getExtensions(): array
    {
        return [
            'wp/post_types' => new ArrayExtension(['my_post_type']),
        ];
    }
}

Clone this wiki locally