How to import Reusable Blocks programatically?

How to import Reusable Blocks programatically?I would supply 20 or 30 .json files with Reusable Blocks and the theme adm

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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745388959a4625570.html

相关推荐

  • How to import Reusable Blocks programatically?

    How to import Reusable Blocks programatically?I would supply 20 or 30 .json files with Reusable Blocks and the theme adm

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信