【发布时间】: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