-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Registration of custom blocks doesn't work when script put in the footer #5661
Comments
Very important issue for plugins integration into Gutenberg structure |
I haven't run into this issue myself, but I suppose there might be some scenario where the order of registered scripts matters. We are looking into the blocks registration and should propose some improvements and possibly breaking changes in the next week or two. |
This is also a similar issue to #4848. |
After investigation with @gziolo we found solution for this issue. There was problem that 3rd party script was included to the page in footer. So if you are registering your own block do not pass Correct: wp_register_script(
'your-script-name',
$this->getBaseUrl() . 'assets/js/admin/guten-modules/guten-modules.bundle.js',
array('wp-blocks', 'wp-element'),
Plugin::VERSION
); Wrong: wp_register_script(
'your-script-name',
$this->getBaseUrl() . 'assets/js/admin/guten-modules/guten-modules.bundle.js',
array('wp-blocks', 'wp-element'),
Plugin::VERSION,
true
); |
As discussed on Slack. This needs to be improved in the long run on the Gutenberg's side to give a freedom where to put the code inside your HTML code. However for the time being it's recommended to avoid putting your plugin's code inside the footer. Sorry for the inconvenience. |
Given that there were other similar changes, it seems like:
is the way to go. |
It should work now regardless of the location of the script (footer vs header) after #6448 got merged. |
Issue Overview
I am discovering Gutenberg source code and hope can contribute after diving into it. After attempt to create a new custom Guten block I found that the code on master branch (51b36b1, last version on March 16) doesn’t allow to create a new block.
You can register your new Guten block with
wp.blocks.registerBlockType
function call. But it won’t work after page reloading.This happens because editor runs before any 3rd party block can be registered. The following script from
lib/client-assets.php
runs the editor and renders editor with content. 3rd party blocks should be registered beforeinitializeEditor
call.Steps to Reproduce (for bugs)
core/freeform
(TinyMCE) block. This happened because your custom block was registered after editor initial run and JS rendered any unknown blocks ascore/freeform
.Unknown block name sets in
blocks/library/index.js:96
.Chrome Version 65.0.3325.162 (Official Build) (64-bit)
Safari Version 11.0.3 (13604.5.6)
I suppose that this is not browser related bug.
Expected Behavior
Custom blocks render as any other core blocks.
Current Behavior
Custom blocks render as
core/freeform
(TinyMCE) block, because custom blocks can't be registered before editor runs.Possible Solution
Update PHP's function
gutenberg_editor_scripts_and_styles
fromlib/client-assets.php
. For example, relocatedo_action( 'enqueue_block_editor_assets' )
which enqueues JS and CSS assets for 3rd party blocks. Move it before inline script which runs the editor. Not sure if it will work.I suppose the best scenario is:
core/*
blocks.3rd-party-blocks/*
.Related Issues and/or PRs
Todos
The text was updated successfully, but these errors were encountered: