This snippet allows you to quickly create WordPress custom post types and taxonomies without writing lengthy registration code. It provides a simple, object-oriented approach with sensible defaults while maintaining full flexibility.
/** * Custom Post Type Creator * * A fluent API for registering WordPress custom post types and taxonomies */ class PostTypeCreator { private $name; private $singular; private $plural; private $options = []; private $taxonomies = []; /** * Create a new post type * * @param string $name The post type name * @param string $singular Singular label * @param string $plural Plural label * @return self */ public function __construct($name, $singular, $plural) { $this->name = $name; $this->singular = $singular; $this->plural = $plural; // Set sensible defaults $this->options = [ 'public' => true, 'has_archive' => true, 'supports' => ['title', 'editor', 'thumbnail'], 'show_in_rest' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-admin-post' ]; return $this; } /** * Register the post type * * @return void */ public function register() { add_action('init', function() { $labels = [ 'name' => $this->plural, 'singular_name' => $this->singular, 'add_new' => 'Add New', 'add_new_item' => 'Add New ' . $this->singular, 'edit_item' => 'Edit ' . $this->singular, 'new_item' => 'New ' . $this->singular, 'view_item' => 'View ' . $this->singular, 'search_items' => 'Search ' . $this->plural, 'not_found' => 'No ' . $this->plural . ' found', 'not_found_in_trash' => 'No ' . $this->plural . ' found in Trash' ]; $args = array_merge($this->options, ['labels' => $labels]); register_post_type($this->name, $args); // Register associated taxonomies foreach ($this->taxonomies as $taxonomy) { $taxonomy->register($this->name); } }); } /** * Add a taxonomy to this post type * * @param string $name Taxonomy name * @param string $singular Singular label * @param string $plural Plural label * @param array $options Optional taxonomy arguments * @return self */ public function taxonomy($name, $singular, $plural, $options = []) { $this->taxonomies[] = new TaxonomyCreator($name, $singular, $plural, $options); return $this; } /** * Set post type options * * @param array $options Post type registration options * @return self */ public function options($options) { $this->options = array_merge($this->options, $options); return $this; } /* Convenience methods for common options */ public function icon($icon) { $this->options['menu_icon'] = 'dashicons-' . $icon; return $this; } public function noArchive() { $this->options['has_archive'] = false; return $this; } } /** * Taxonomy Creator helper class */ class TaxonomyCreator { private $name; private $singular; private $plural; private $options = []; public function __construct($name, $singular, $plural, $options = []) { $this->name = $name; $this->singular = $singular; $this->plural = $plural; // Sensible defaults $this->options = array_merge([ 'hierarchical' => true, 'show_admin_column' => true, 'show_in_rest' => true ], $options); } public function register($post_type) { $labels = [ 'name' => $this->plural, 'singular_name' => $this->singular, 'search_items' => 'Search ' . $this->plural, 'all_items' => 'All ' . $this->plural, 'parent_item' => 'Parent ' . $this->singular, 'parent_item_colon' => 'Parent ' . $this->singular . ':', 'edit_item' => 'Edit ' . $this->singular, 'update_item' => 'Update ' . $this->singular, 'add_new_item' => 'Add New ' . $this->singular, 'new_item_name' => 'New ' . $this->singular . ' Name' ]; $args = array_merge($this->options, ['labels' => $labels]); register_taxonomy($this->name, $post_type, $args); } } /** * USAGE EXAMPLE: * * // Create a "Projects" post type * $projects = new PostTypeCreator('project', 'Project', 'Projects'); * $projects->taxonomy('project-category', 'Category', 'Categories') * ->taxonomy('project-tag', 'Tag', 'Tags', ['hierarchical' => false]) * ->icon('portfolio') * ->register(); */