【问题标题】:How can I build a wordpress gutenberg multiple blocks plugin that support dynamic content如何构建支持动态内容的 wordpress gutenberg 多块插件
【发布时间】:2021-12-28 20:02:17
【问题描述】:

我正在使用一组自定义 Gutenberg 块创建自定义 Wordpress 主题。以前我将所有块作为单独的插件工作,然后我开始将它们组合成一个插件,因为我认为它更容易维护和更新。我的旧文件夹结构如下所示:

wp-content
 |
 +-- plugins
 |  |
 |  +-- dir 1
 |  |  +-- build
 |  |  +-- node_modules
 |  |  +-- src
 |  |  |  +-- index.js (includes edit and save functions)
 |  |
 |  +-- dir 2
 |  |  +-- build
 |  |  +-- node_modules
 |  |  +-- src
 |  |  |  +-- index.js (includes edit and save functions)

我的新文件夹结构是这样的(从这个项目中学到的:https://github.com/rmorse/multiple-blocks-plugin

wp-content
 |
 +-- plugins
 |  |
 |  +-- multiple-blocks-plugin
 |  |  |
 |  |  +-- blocks
 |  |  |  |
 |  |  |  +-- dir 1
 |  |  |  |  +-- build
 |  |  |  |  +-- src
 |  |  |  |  |  +-- edit.js
 |  |  |  |  |  +-- index.js
 |  |  |  |  |  +-- save.js
 |  |  |  |  +-- block.json
 |  |  |  |
 |  |  |  +-- dir 2
 |  |  |  |  +-- build
 |  |  |  |  +-- src
 |  |  |  |  |  +-- edit.js
 |  |  |  |  |  +-- index.js
 |  |  |  |  |  +-- save.js
 |  |  |  |  +-- block.json
 |  |  |  |
 |  |  |  +-- dir 3
 |  |  |  |  +-- build
 |  |  |  |  +-- src
 |  |  |  |  |  +-- edit.js
 |  |  |  |  |  +-- index.js
 |  |  |  |  +-- block.json
 |  |  |  |  +-- index.php (the callback function is stored here)
 |  |  | 
 |  |  +-- build
 |  |  +-- node_modules
 |  |  +-- multiple-blocks-plugin.php

multiple-blocks-plugin.php 文件中,我维护了我使用register_block_type() 函数注册的所有插件的列表。这些函数告诉 Wordpress 查找 block.json 文件,我的所有设置和属性现在都存储在其中。我的一个块是使用动态内容(一个应该始终显示最新三个帖子的新闻块)。我的主要问题是当函数存储在新文件夹结构中时,我无法让我的新设置读取callback_function。我的目标是将所有块重建为动态的,但我想维护一个插件 + 多块设置。

这是为/blocks/news/注册所有不同块和故障块的函数

function create_block_multiple_blocks_block_init()
{
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/column-block/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/column-block-prices/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/column-block-services/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/column-block-specialists/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/counter/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/counter-list/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/cta-banner/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/gallery/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/gallery-image/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/image-text-block/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/hero/');
  register_block_type_from_metadata(plugin_dir_path(__FILE__) . 'blocks/news/', array(
    "callback_function" => "news_register_block"
  ));
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/opening-hours/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/opening-hours-list/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/process/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/process-list/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/why-choose-us/');
  register_block_type(plugin_dir_path(__FILE__) . 'blocks/why-choose-us-list/');
}
add_action('init', 'create_block_multiple_blocks_block_init');

问题

如何设置使用动态块的项目,其中所有块都存储在单个插件中?我对 Wordpress 开发很陌生——我习惯于在 Sitecore 工作——我很想学习如何最好地建立这样一个项目。谢谢。

【问题讨论】:

    标签: wordpress plugins wordpress-gutenberg gutenberg-blocks


    【解决方案1】:

    我会使用 DirectoryIterator 尽可能懒惰并扫描“块”目录。也许我会看看是否存在 block.json,然后让它注册这个目录。

    foreach ( $blocks = new DirectoryIterator( __DIR__ . '/blocks' ) as $item ) {
        if ( $item -> isDir() && !$item -> isDot() ) if ( file_exists( $item -> getPathname() . '/block.json' ) ) register_block_type( $item -> getPathname() ); 
    }
    

    下一步可能是包含一个名称与文件夹完全相同的 php 文件。在该文件夹中,您存储了一个 name-of-the-folder_render_callback 函数。如果所述文件存在,则包含它并在 register_block_type 参数中使用 name-of-the-folder_render_callback。

    foreach ( $blocks = new DirectoryIterator( __DIR__ . '/blocks' ) as $item ) {
        if ( $item -> isDir() && !$item -> isDot() ) if ( file_exists( $item -> getPathname() . '/block.json' ) ) {
            if ( file_exists( $item -> getPathname() . '/' . $item -> getBasename() . '.php' ) ) {
                include( $item -> getPathname() . '/' . $item -> getBasename() . '.php' );
                register_block_type( $item -> getPathname() , array( 'render_callback' => $item -> getBasename() . '_render_callback' ) );
            } else {
                register_block_type( $item -> getPathname() );
            }
        }
    }
    

    但未经测试。

    【讨论】:

    • 嗨塞巴斯蒂安。谢谢您的答复。我喜欢你提出的循环,它使代码更加紧凑。我尝试按照您的建议包含 PHP 文件,但我仍然无法在该特定文件中执行任何函数。你能提供一个name-of-the-folder.php文件内容的例子,包括回调函数吗?
    • 你好,塞巴斯蒂安。我逐行浏览了您的代码,发现了两个错误。修复它们后,它终于可以工作了!一周的挫败感终于结束了。在您的第二个 if 语句中,您有两个 file_exists 检查,并且在您的 register_block_type 中,您使用的是“callback_function”而不是“render_callback”。尽管如此,感谢您的快速指导!
    • 嘿丹尼,是的,你是对的。我更正了。
    猜你喜欢
    • 2019-03-01
    • 1970-01-01
    • 2010-11-24
    • 2019-05-19
    • 1970-01-01
    • 2018-10-06
    • 2018-07-18
    • 2019-05-12
    • 1970-01-01
    相关资源
    最近更新 更多