主题中的古腾堡块:最初将您自己的块作为插件集成到主题和错误提示中

发布于 2025-01-17 09:35:22 字数 1677 浏览 0 评论 0原文

我最初创建了一个块作为插件,使用最新的 wordpress block.json 架构,该架构在 gutenberg 中工作并出现,但是当将其集成到主题时,我收到了 url 请求错误的错误。

add_action('init', 'register_my_block');
function register_my_block(){
    register_block_type(
        __DIR__. '/myblocks', 
        array(        
            'render_callback' => 'render_my_block_posts'
    ));
}

还使用 register_block_type_from_metadata() 但得到了相同的错误

控制台中的示例错误:

https://domain.local/wp-content/plugins/D:/xampp/htdocs/wp-content/themes/NewsPaper/inc/gutenberg/myblocks/index.js?ver=d395fe5d9d90bd6565685c91d8d38888

对文件中函数的 php 文件进行了正确的调用,但在注册请求时,该块出错了

代码来自块.json

{
    "$schema": "https://json.schemastore.org/block.json",
    "apiVersion": 2,
    "name": "block/custompost",
    "version": "1.0.0",
    "title": "CustomPosts",
    "category": "widget",
    "icon": "grid-view",
    "description": "Display a custom post.",
    "keywords": [ "grid" , "gridview" , "custompost" ],
    "textdomain": "default",
    "attributes": {
        "columns": {
            "type": "number",
            "default": 3
        },
        "rows": {
            "type": "number",
            "default": 9
        },
        "display": {
            "type": "string",
            "default": ""
        },
        "display_setting": {
            "type": "array",
            "default": []
        "edit_mode": {
            "type": "boolean",
            "default": true
        }
    },
    "supports": {
        "align": [ "wide" , "full"],
        "html": false
    },

    "editorScript": "file:./index.js",
    "editorStyle": "file:./custompost.css"
}

I created a block initially as a plugin with the latest wordpress block.json architecture that worked and appeared within gutenberg but when integrating it to the theme I get an error of the url request error.

add_action('init', 'register_my_block');
function register_my_block(){
    register_block_type(
        __DIR__. '/myblocks', 
        array(        
            'render_callback' => 'render_my_block_posts'
    ));
}

Also use register_block_type_from_metadata() but got the same error

Example error in console:

https://domain.local/wp-content/plugins/D:/xampp/htdocs/wp-content/themes/NewsPaper/inc/gutenberg/myblocks/index.js?ver=d395fe5d9d90bd6565685c91d8d38888

A correct call is made to the php file of the functions to the file but when registering the request it goes wrong for the block

The code from block.json

{
    "$schema": "https://json.schemastore.org/block.json",
    "apiVersion": 2,
    "name": "block/custompost",
    "version": "1.0.0",
    "title": "CustomPosts",
    "category": "widget",
    "icon": "grid-view",
    "description": "Display a custom post.",
    "keywords": [ "grid" , "gridview" , "custompost" ],
    "textdomain": "default",
    "attributes": {
        "columns": {
            "type": "number",
            "default": 3
        },
        "rows": {
            "type": "number",
            "default": 9
        },
        "display": {
            "type": "string",
            "default": ""
        },
        "display_setting": {
            "type": "array",
            "default": []
        "edit_mode": {
            "type": "boolean",
            "default": true
        }
    },
    "supports": {
        "align": [ "wide" , "full"],
        "html": false
    },

    "editorScript": "file:./index.js",
    "editorStyle": "file:./custompost.css"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2025-01-24 09:35:22

我遇到了完全相同的问题,似乎该功能对于版本WordPress 5.9中的主题不可用。我刚刚像Worpress 6.0一样对此进行了测试,并且可以按预期工作。

在WordPress的先前版本中,它纯粹是用于插件

I had the exact same issue, it appears to be this function is not available for themes in version Wordpress 5.9. I have just tested this exactly as is with Worpress 6.0 and it works as expected.

In prior version of wordpress it is purely for plugins, hence why it is looking at wp-content/plugins and then appending the url/path

╰沐子 2025-01-24 09:35:22

在控制台错误中,目录是“ myblock” >)。您的问题可能是由于一个小型错字引起索引。

In the console error, the directory is "myblock" where as in the register_block_type(...) its "myblocks" (with a s). Potentially your issue is due to a small typo causing index.js to not be found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文