CSS style file from custom block plugin is not being enqueued after updating to v8.4.0
Describe the bug I've made a custom block plugin and everything worked fine until I've updated Gutenberg plugin to version 8.4.0 from 8.3.0. Now, CSS file from my plugin is not being enqueued on the frontend. On the post edit screen that css file is enqueued and look OK.
Editor version (please complete the following information): I'm using the latest WP version with the Gutenberg plugin installed.
Does it enqueue properly when the block is included on the page? We are testing optimization that loads block’s CSS on the front-end only when it’s used.
Even if the block is on the page the CSS file is not included. Not sure how this new optimization works, maybe it works with single block plugins but not with multi-blocks plugins.
Does it mean that multiple blocks share the same style and only one of them enqueues this this style? I’m trying to understand what use case are you describing.
I'm using the Create Guten Block by Ahmad Awais to make custom blocks, so there is only one CSS file and one js script that register all the blocks at once.
I've checked the Atomic Blocks plugin and there is the same problem with not enqueuing CSS file on the frontend.
Can you share the part of the code that registers and enqueues this CSS?
Here is the content of the init file:
function mp_blocks_assets() {
// Register block styles for both frontend + backend.
wp_register_style(
'mp-blocks-style', // Handle.
plugins_url( 'dist/blocks.style.build.css', dirname( __FILE__ ) ), // Block style CSS.
array(), // Dependency to include the CSS after it.
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time.
);
// Register block editor styles for backend.
wp_register_style(
'mp-blocks-editor-style', // Handle.
plugins_url( 'dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS.
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
);
// Register block editor script for backend.
wp_register_script(
'mp-blocks-editor-script', // Handle.
plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack.
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above.
null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time.
true // Enqueue the script in the footer.
);
// WP Localized globals. Use dynamic PHP stuff in JavaScript via `cgbGlobal` object.
wp_localize_script(
'mp-blocks-editor-script',
'cgbGlobal', // Array containing dynamic data for a JS Global.
[
'pluginDirPath' => plugin_dir_path( __DIR__ ),
'pluginDirUrl' => plugin_dir_url( __DIR__ ),
// Add more data here that you want to access from `cgbGlobal` object.
]
);
register_block_type(
'mp/mp-blocks', array(
// Enqueue blocks.style.build.css on both frontend & backend.
'style' => 'mp-blocks-style',
// Enqueue blocks.editor.build.css in the editor only.
'editor_style' => 'mp-blocks-editor-style',
// Enqueue blocks.build.js in the editor only.
'editor_script' => 'mp-blocks-editor-script',
)
);
}
add_action( 'init', 'mp_blocks_assets' );
function mp_register_block_categories( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => 'mp-blocks',
'title' => __( 'MP Blocks', 'mp-blocks' ),
),
)
);
}
add_filter( 'block_categories', 'mp_register_block_categories', 10, 2 );
It must be related to #22754 where people started reporting similar issues after 8.4.0 release. I think the best way to go is to revert those changes and try a different approach in WordPress core.
Now that #22754 was reverted, it shouldn't be an issue after the Gutenberg plugin release planned for later this week.