skip to Main Content

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"
}

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search