Created
September 27, 2022 16:16
-
-
Save Ciantic/434385eedb0b5985fa6544120dc40d3d to your computer and use it in GitHub Desktop.
Register reusable blocks as patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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