lzb/import/template
Runs after one block template has been imported from a JSON file, which is where an imported template gets pointed at the post types this site actually has.
It fires at the end of import_template(), once the lazyblocks_templates post exists and its four metas have been written. Import stops before this point when a template for any of the same post types is already registered, so the action only fires for templates that were created. A file holding several templates fires it once per template.
Attributes
| Name | Type | Description |
|---|---|---|
$post_id | Int | post ID of the template just created |
$data | Array | the template as it appeared in the file |
$data has id, title, post_types, blocks and template_lock. id is the post ID from the site the template was exported from. The metas written from it are _lzb_template_post_types, _lzb_template_blocks (URL encoded JSON), _lzb_template_lock and _lzb_template_convert_blocks_to_content, which the importer sets to true while the template editor's own default is false.
Usage
function my_lzb_import_template( $post_id, $data ) {
// The export was made on a site with a "case-study" post type this one
// calls "project". Retarget the template instead of failing quietly.
$post_types = array_map(
function ( $type ) {
return 'case-study' === $type ? 'project' : $type;
},
(array) $data['post_types']
);
update_post_meta( $post_id, '_lzb_template_post_types', $post_types );
}
add_action( 'lzb/import/template', 'my_lzb_import_template', 10, 2 );_lzb_template_post_types is an array of post type slugs, and a template pointing at a post type that does not exist is simply never applied, with no notice anywhere. _lzb_template_blocks has to stay URL encoded JSON, so run any replacement through rawurlencode( wp_json_encode( ... ) ) the way the importer does.
To rewrite the payload before any post is inserted, use lzb/import_json. The block equivalent of this action is lzb/import/block, and templates themselves are covered in Templates.