You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Love the print_block function. Great for scaffolding or hardcoding some blocks in templates. What would the best practice be for enqueuing the block's css/js? When using a block normally, as you know, the css/js is enqueued. I was thinking of adding the css/js to the global css/scripts but that doesn't seem like the best solution. We would ideally want it to load only if the block is utilized.
I did modify the print_block function to enqueue but unsure if it's the best way to do it. It does the job but isn't my favorite. If this block is used in gutenberg, then the css / js is called twice. I was trying to think of a way to prevent that but I'm not sure of the block handles to prevent multiple enqueues of the same css/js.
function print_block( $block_name = '', $args = [] ) {
if ( ! $block_name ) {
return;
}
// extract args.
if ( ! empty( $args ) ) {
extract( $args ); //phpcs:ignore WordPress.PHP.DontExtract.extract_extract -- We can use it here since we know what to expect on the arguments.
}
wp_enqueue_style(
"$block_name-style",
get_theme_file_uri( "build/blocks/$block_name/style-script.css" ),
array(),
1.0
);
wp_enqueue_script(
"$block_name-script",
get_theme_file_path( "build/blocks/$block_name/script.js" ),
array(),
1.0
);
require get_template_directory() . '/src/blocks/' . $block_name . '/' . $block_name . '.php';
}
Any thoughts?
The text was updated successfully, but these errors were encountered:
Hi,
Love the print_block function. Great for scaffolding or hardcoding some blocks in templates. What would the best practice be for enqueuing the block's css/js? When using a block normally, as you know, the css/js is enqueued. I was thinking of adding the css/js to the global css/scripts but that doesn't seem like the best solution. We would ideally want it to load only if the block is utilized.
I did modify the print_block function to enqueue but unsure if it's the best way to do it. It does the job but isn't my favorite. If this block is used in gutenberg, then the css / js is called twice. I was trying to think of a way to prevent that but I'm not sure of the block handles to prevent multiple enqueues of the same css/js.
Any thoughts?
The text was updated successfully, but these errors were encountered: