最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to import Reusable Blocks programatically?

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论