concept-image / wp-make-cpt
Add a command to generate custom post types and taxonomies.
Requires
- php: >=8.0
- concept-image/wp-archive-base: >=2.0
- concept-image/wp-custom-post-types: >=2.0
- fakerphp/faker: >=1.24
- log1x/acf-composer: >=3.3
- roots/acorn: >=4.3
Suggests
- concept-image/wp-blocks: >=2.0
- concept-image/wp-make: >=2.0
README
Requirements
- PHP >= 8.0
- WordPress >= 6.5
- Roots Bedrock >= 1.26
- Roots Acorn >= 4.3
Installation
You can install this package with Composer:
composer require concept-image/wp-make-cpt --dev
Purpose
This package provides commands to generate custom post types and taxonomies in WordPress projects using Acorn.
Available Commands
make:cpt
Create a new Custom Post Type class file. (Before creating a new CPT, check if a preset is available for the one you wish to create.)
Options:
name
(required): The name of the custom post type.--lang
: Language for the custom post type.--singular
: Singular name for the custom post type.--plural
: Plural name for the custom post type.--is_feminine
: Indicates if the custom post type is feminine.--dashicon
: Dashicon for the custom post type.--public
: Indicates if the custom post type is public.--has_archive
: Indicates if the custom post type has an archive.--hierarchical
: Indicates if the custom post type is hierarchical.--supports
: Features supported by the custom post type.--taxonomies
: Taxonomies associated with the custom post type.--rewrite
: Indicates if the custom post type should be rewritten.--slug
: Slug for the custom post type.--show_ui
: Indicates if the custom post type should be shown in the UI.--position
: Position of the custom post type in the menu.--show_in_rest
: Indicates if the custom post type should be shown in the REST API.--delete_with_user
: Indicates if the custom post type should be deleted with the user.
make:tax
Create a new Taxonomy class file.
Options:
name
(required): The name of the taxonomy.--lang
: Language for the taxonomy.--singular
: Singular name for the taxonomy.--plural
: Plural name for the taxonomy.--is_feminine
: Indicates if the taxonomy is feminine.--object_type
: Object type for the taxonomy.--public
: Indicates if the taxonomy is public.--hierarchical
: Indicates if the taxonomy is hierarchical.--rewrite
: Indicates if the taxonomy should be rewritten.--rewrite_hierarchical
: Indicates if the taxonomy should be rewritten hierarchically.--slug
: Slug for the taxonomy.--show_ui
: Indicates if the taxonomy should be shown in the UI.--show_in_rest
: Indicates if the taxonomy should be shown in the REST API.--show_in_nav_menus
: Indicates if the taxonomy should be shown in navigation menus.
Examples
Create a custom post type named "project":
wp acorn make:cpt project
Create a taxonomy named "project_cat":
wp acorn make:tax project_cat
Command Prompts and Choices
When you run the make:cpt
or make:tax
commands, you will be prompted to provide various details about the custom post type or taxonomy. Here are the steps and choices you will encounter:
- Language: You will be asked to select the language code (e.g.,
en
orfr
). - Singular Name: Enter the singular name of the custom post type or taxonomy.
- Plural Name: Enter the plural name of the custom post type or taxonomy.
- Feminine (French only): Indicate if the name is feminine or masculine.
- Dashicon (Custom Post Type only): Enter the dashicon for the custom post type.
- More Options: Indicate if you want to add more options.
- Public: Indicate if the custom post type or taxonomy is public.
- Has Archive (Custom Post Type only): Indicate if the custom post type has an archive.
- Hierarchical: Indicate if the custom post type or taxonomy is hierarchical.
- Supports (Custom Post Type only): Select the features supported by the custom post type.
- Taxonomies (Custom Post Type only): Select the taxonomies associated with the custom post type.
- Rewrite: Indicate if the custom post type or taxonomy should be rewritten.
- Slug: Enter the slug for the custom post type or taxonomy.
- Show UI: Indicate if the custom post type or taxonomy should be shown in the UI.
- Position (Custom Post Type only): Enter the position of the custom post type in the menu.
- Show in REST API: Indicate if the custom post type or taxonomy should be shown in the REST API.
- Delete with User (Custom Post Type only): Indicate if the custom post type should be deleted with the user.
- Show in Navigation Menus (Taxonomy only): Indicate if the taxonomy should be shown in navigation menus.
- Object Type (Taxonomy only): Select the post types to link the taxonomy to.
These prompts ensure that you provide all necessary information to generate the custom post type or taxonomy with the desired configuration.
Generated Files
Custom Post Type
When you run the make:cpt
command, the following files are generated:
- Custom Post Type Class: Located in
App\CustomPostTypes\{{ className }}.php
, this file defines the custom post type and its arguments. - Views: Blade templates for the custom post type views, such as
archive
,single
, andcard
. - Styles: SCSS files for the custom post type styles.
- ACF Fields: PHP files defining ACF fields for the custom post type.
Taxonomy
When you run the make:tax
command, the following files are generated:
- Taxonomy Class: Located in
App\Taxonomies\{{ className }}.php
, this file defines the taxonomy and its arguments. - Views: Blade templates for the taxonomy views, such as
taxonomy-view
. - ACF Fields: PHP files defining ACF fields for the taxonomy.
Available Presets for Custom Post Types
The following presets are available for custom post types: news and event. A preset contains all basic files for a difine CPT.
Available Commands
cpt-preset
Generate dummy Custom Post Type data with Faker. If the name of the preset is not provided, the command will list all available presets.
Options:
name
: The name of the preset you want to publish.--force
: Force to republish the preset files.
Usage of the Seed Command
The seed command allows you to generate dummy data for custom post types and taxonomies using Faker.
Available Commands
seed:cpt
Generate dummy Custom Post Type data with Faker.
Options:
name
(required): The name of the custom post type.count
: The number of dummy posts to generate (default: 24).
seed:tax
Generate dummy Taxonomy data with Faker.
Options:
name
(required): The name of the taxonomy.--count
: The number of dummy terms to generate (default: 4).
seed:event
Generate dummy Events data with faker.
Options:
--count
: The number of dummy terms to generate (default: 4).
Examples
Generate 10 dummy posts for the custom post type "project":
wp acorn seed:cpt project --count=10
Generate 5 dummy terms for the taxonomy "project_cat":
wp acorn seed:tax project_cat --count=5
Results
When you run the seed:cpt
command, the following actions are performed:
- Dummy Posts: Generates the specified number of dummy posts for the custom post type.
- Taxonomies: Assigns random terms from associated taxonomies to the dummy posts.
- ACF Fields: Populates ACF fields with random data if defined for the custom post type.
When you run the seed:tax
command, the following actions are performed:
- Dummy Terms: Generates the specified number of dummy terms for the taxonomy.
Custom Seeder
To build a custom seeder, you can create a new Command Class that extends CustomPostTypeSeedCommand and override the getValueForFieldName
function in the CustomPostTypeSeedCommand
class. This allows you to provide custom Faker data for specific fields.
Define Custom Post Type for the seeder
// filepath: /App/Commands/Seeders/ExampleSeederCommand
protected string $objectName = 'example'
Example
Suppose you have a custom field named address
and you want to generate a random sentence for this field. You can override the getValueForFieldName
function as follows:
// filepath: /App/Commands/ExampleSeederCommand
protected function getValueForFieldName(string $name, Generator $faker)
{
return match ($name) {
'address' => $faker->locale('fr_FR')->address;,
default => null,
};
}
In this example, the address
will be populated with a random address generated by Faker. You can add more cases to the match
statement to handle other custom fields as needed.
Changelog
Please refer to CHANGELOG for more information.
Contributing
Please refer to CONTRIBUTING for more information.
License
Please refer to LICENSE for more information.