I am trying to write a plugin that packages a custom post type with a collection of custom taxonomies, provides all the appropriate default block templates, and applies them automatically. I specifically would prefer to insert any template into the hierarchy and not override any expected process. It is my understanding that as of late last year this is possible, but I'm struggling to execute it correctly. I've been able to register the block template and apply it, but only to the broad category of template part (like "archive") and not the specific place I need (the custom post type archive).
I'm registering a taxonomy ("format) and post type ("offering") as expected:
function register_format_taxonomy() {
$labels = array(
'name' => _x( 'Formats', 'taxonomy general name', 'domain-offerings' ),
'singular_name' => _x( 'Format', 'taxonomy singular name', 'domain-offerings' ),
'search_items' => __( 'Search Formats', 'domain-offerings' ),
'all_items' => __( 'All Formats', 'domain-offerings' ),
'view_item' => __( 'View Format', 'domain-offerings' ),
'parent_item' => __( 'Parent Format', 'domain-offerings' ),
'parent_item_colon' => __( 'Parent Format:', 'domain-offerings' ),
'edit_item' => __( 'Edit Format', 'domain-offerings' ),
'update_item' => __( 'Update Format', 'domain-offerings' ),
'add_new_item' => __( 'Add New Format', 'domain-offerings' ),
'new_item_name' => __( 'New Format Name', 'domain-offerings' ),
'not_found' => __( 'No Formats Found', 'domain-offerings' ),
'back_to_items' => __( 'Back to Formats', 'domain-offerings' ),
'menu_name' => __( 'Formats', 'domain-offerings' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'offering/formats' ),
'show_in_rest' => true,
);
register_taxonomy( 'formats', 'offerings', $args );
}
add_action( 'init', 'register_format_taxonomy', 0 );
function register_offerings_post_type() {
$labels = array(
'name' => _x( 'Offerings', 'post type general name', 'domain-offerings' ),
'singular_name' => _x( 'Offering', 'post type singular name', 'domain-offerings' ),
'add_new' => _x( 'Add New', 'offering', 'domain-offerings' ),
'add_new_item' => __( 'Add New Offering', 'domain-offerings' ),
'edit_item' => __( 'Edit Offering', 'domain-offerings' ),
'new_item' => __( 'New Offering', 'domain-offerings' ),
'all_items' => __( 'All Offerings', 'domain-offerings' ),
'view_item' => __( 'View Offering', 'domain-offerings' ),
'search_items' => __( 'Search Offerings', 'domain-offerings' ),
'not_found' => __( 'No Offerings Found', 'domain-offerings' ),
'not_found_in_trash' => __( 'No Offerings Found in the Trash', 'domain-offerings' ),
'parent_item_colon' => 'Offering:',
'menu_name' => 'Offerings',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'offerings' ),
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-learn-more',
'capability_type' => 'post',
'supports' => array( 'title', 'editor', 'page-attributes', 'thumbnail', 'excerpt' ),
'has_archive' => true,
'show_in_rest' => true,
);
register_post_type( 'offerings', $args );
}
add_action( 'init', 'register_offerings_post_type' );
Currently I've only gotten as far as trying to address the offerings
archive template, which I try to manage like this:
function register_offerings_archive() {
register_block_template(
'offerings//offerings-archive',
array(
'title' => 'Plugin Template',
'description' => 'A template registered by a plugin.',
'content' => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template.</p><!-- /wp:paragraph --></main><!-- /wp:group -->',
)
);
}
add_action( 'init', 'register_offerings_archive' );
function insert_offerings_archive( ) {
return array( 'offerings-archive' );
}
add_action( 'archive_template_hierarchy', 'insert_offerings_archive');
I assume this is not the right way to do this as it applies the template to all archives, and I know I need to at least be inserting the registered template into an existing array somewhere. Can anyone offer help and point me in the right direction please?
I am trying to write a plugin that packages a custom post type with a collection of custom taxonomies, provides all the appropriate default block templates, and applies them automatically. I specifically would prefer to insert any template into the hierarchy and not override any expected process. It is my understanding that as of late last year this is possible, but I'm struggling to execute it correctly. I've been able to register the block template and apply it, but only to the broad category of template part (like "archive") and not the specific place I need (the custom post type archive).
I'm registering a taxonomy ("format) and post type ("offering") as expected:
function register_format_taxonomy() {
$labels = array(
'name' => _x( 'Formats', 'taxonomy general name', 'domain-offerings' ),
'singular_name' => _x( 'Format', 'taxonomy singular name', 'domain-offerings' ),
'search_items' => __( 'Search Formats', 'domain-offerings' ),
'all_items' => __( 'All Formats', 'domain-offerings' ),
'view_item' => __( 'View Format', 'domain-offerings' ),
'parent_item' => __( 'Parent Format', 'domain-offerings' ),
'parent_item_colon' => __( 'Parent Format:', 'domain-offerings' ),
'edit_item' => __( 'Edit Format', 'domain-offerings' ),
'update_item' => __( 'Update Format', 'domain-offerings' ),
'add_new_item' => __( 'Add New Format', 'domain-offerings' ),
'new_item_name' => __( 'New Format Name', 'domain-offerings' ),
'not_found' => __( 'No Formats Found', 'domain-offerings' ),
'back_to_items' => __( 'Back to Formats', 'domain-offerings' ),
'menu_name' => __( 'Formats', 'domain-offerings' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'offering/formats' ),
'show_in_rest' => true,
);
register_taxonomy( 'formats', 'offerings', $args );
}
add_action( 'init', 'register_format_taxonomy', 0 );
function register_offerings_post_type() {
$labels = array(
'name' => _x( 'Offerings', 'post type general name', 'domain-offerings' ),
'singular_name' => _x( 'Offering', 'post type singular name', 'domain-offerings' ),
'add_new' => _x( 'Add New', 'offering', 'domain-offerings' ),
'add_new_item' => __( 'Add New Offering', 'domain-offerings' ),
'edit_item' => __( 'Edit Offering', 'domain-offerings' ),
'new_item' => __( 'New Offering', 'domain-offerings' ),
'all_items' => __( 'All Offerings', 'domain-offerings' ),
'view_item' => __( 'View Offering', 'domain-offerings' ),
'search_items' => __( 'Search Offerings', 'domain-offerings' ),
'not_found' => __( 'No Offerings Found', 'domain-offerings' ),
'not_found_in_trash' => __( 'No Offerings Found in the Trash', 'domain-offerings' ),
'parent_item_colon' => 'Offering:',
'menu_name' => 'Offerings',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'offerings' ),
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-learn-more',
'capability_type' => 'post',
'supports' => array( 'title', 'editor', 'page-attributes', 'thumbnail', 'excerpt' ),
'has_archive' => true,
'show_in_rest' => true,
);
register_post_type( 'offerings', $args );
}
add_action( 'init', 'register_offerings_post_type' );
Currently I've only gotten as far as trying to address the offerings
archive template, which I try to manage like this:
function register_offerings_archive() {
register_block_template(
'offerings//offerings-archive',
array(
'title' => 'Plugin Template',
'description' => 'A template registered by a plugin.',
'content' => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template.</p><!-- /wp:paragraph --></main><!-- /wp:group -->',
)
);
}
add_action( 'init', 'register_offerings_archive' );
function insert_offerings_archive( ) {
return array( 'offerings-archive' );
}
add_action( 'archive_template_hierarchy', 'insert_offerings_archive');
I assume this is not the right way to do this as it applies the template to all archives, and I know I need to at least be inserting the registered template into an existing array somewhere. Can anyone offer help and point me in the right direction please?
As far as I can tell you shouldn't need archive_template_hierarchy
, just name the template properly according to the template hierarchy. So offerings//archive-offerings
.