# Post Types

A shorthand for registering post types via a JSON file instead of a whole bunch of PHP to make life just a little bit easier.

# Usage

This feature is enabled automatically with the plugin.

You will also need to define a JSON file which configures your post-types. This should be caleld post-types.json and live in the root of your theme (wp-content/themes/{theme_name}/post-types.json).

{
	"$schema": "https://raw.githubusercontent.com/boxuk/wp-packages/main/schema/post-type.schema.json",
	"post_types": {
		"project": {
			"has_archive": true,
			"labels": {
				"name": "Projects",
				"singular_name": "Project"
			},
			"menu_icon": "dashicons-lightbulb",
			"taxonomies": [ "category", "post_tag" ],
			"supports": [ "title", "editor", "thumbnail", "excerpt" ]
		},
		"product": { 
			"menu_icon": "dashicons-cart",
			"labels": {
				"name": "Products",
				"singular_name": "Product"
			},
        }
    }
}

You can modify the path of this file using the boxuk_post_types_json_file_path filter.

add_filter(
	'boxuk_post_types_json_file_path',
	function ( string $default_path ): string { 
		return __DIR__ . '/my-path-to/post-types.json';
	}
);

The schema file will help your IDE populate any fields you think you need. After parsing, the file is passed to register_post_type() so the WordPress documentation should be useful!

# Registering templates for new posts

The block-editor allows you to specify a template of which blocks should be automatically inserted for all new posts created. This shouldn't be confused with the also-named 'Templates', which are the the templates in which the content of the post is within.

The new-post-templates are a collection of blocks that are pre-inserted when the editor loads and can speed up content entry. You can define these using the array syntax in your JSON file, but this becomes very verbose and difficult to manage.

Alternatively, you can add a file to your theme (wp-content/themes/{theme_name}/post-type-templates/{post_type_slug}.html) which includes the gutenberg markup in HTML format. A quick way to build these is to load the editor in your browser, build the template out as you would normally. Enable 'Code Editor View' in the block editor, and you can copy the HTML that has been generated by the blocks. This can be pasted in your file and saved. You may wish to replace any hardcoded image URLs with placeholder image URLs.