Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created September 27, 2022 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/434385eedb0b5985fa6544120dc40d3d to your computer and use it in GitHub Desktop.
Save Ciantic/434385eedb0b5985fa6544120dc40d3d to your computer and use it in GitHub Desktop.
Register reusable blocks as patterns
<?php
// Show menu item for reusable blocks
add_action('admin_menu', function () {
add_menu_page(_x('Reusable blocks', 'post type general name'), _x('Reusable blocks', 'post type general name'), 'edit_posts', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22);
});
add_action("should_load_remote_block_patterns", function () {
// I chose `should_load_remote_block_patterns` action because it's triggered
// only when patterns are loaded in REST call, thus no useless querying when not required
$q = new WP_Query([
"post_type" => "wp_block",
"posts_per_page" => -1,
]);
foreach ($q->get_posts() as $p) {
register_block_pattern("wp_block/post-{$p->ID}", [
"title" => $p->post_title,
"description" => "",
"content" => $p->post_content
]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment