How to import Reusable Blocks programatically?
I would supply 20 or 30 .json files with Reusable Blocks and the theme admin would be able to use them in their posts or pages.
How could I import them programatically on theme activation?
How to import Reusable Blocks programatically?
I would supply 20 or 30 .json files with Reusable Blocks and the theme admin would be able to use them in their posts or pages.
How could I import them programatically on theme activation?
Share Improve this question asked Jun 19, 2019 at 10:27 CiprianCiprian 8802 gold badges11 silver badges27 bronze badges 2- Have you looked into how the import currently works? There'll probably be a function call somewhere which you can use in your theme activation – kero Commented Jun 19, 2019 at 11:53
- True, but I decided to create them as custom post types, as the import function might change at some point. – Ciprian Commented Jun 19, 2019 at 13:29
1 Answer
Reset to default 1The easiest solution so far seems to be creating the Reusable Blocks as custom post types:
wp_insert_post([
'post_content' => '<!-- wp:shortcode -->
[slider]
<!-- /wp:shortcode -->',
'post_title' => 'My Slider',
'post_type' => 'wp_block',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'guid' => sprintf(
'%s/wp_block/%s',
site_url(),
sanitize_title('my-slider')
)
]);
This way, I can create a library of blocks, loop and import them all.
Probably, as @kero mentioned, the core JSON import works the same internally.