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
In a block theme, I would like to be able to determine if the template includes a block. In post content the has_block() function does that. My use case is to enqueue some JS in the case the pagination block is in the template.
In a block theme, all blocks are parsed before the <head>. What that means, is ou can hook in render_block, and enqueue your script if the block exists.
Example:
add_filter( 'render_block', function( $block_content, $block ) {
// Make sure we have the blockName.if ( empty( $block['blockName'] ) ) {
return$block_content;
}
// If this is a pagination block, enqueue the pagination script.if (
'core/query-pagination' === $block['blockName'] ||
'core/query-pagination-next' === $block['blockName'] ||
'core/query-pagination-previous' === $block['blockName'] ||
'core/query-pagination-numbers' === $block['blockName']
) {
wp_enqueue_script( 'my-pagination-script' );
}
// Return the block content.return$block_content;
}, 10, 2 );
What problem does this address?
In a block theme, I would like to be able to determine if the template includes a block. In post content the has_block() function does that. My use case is to enqueue some JS in the case the pagination block is in the template.
This is similar to #14758
What is your proposed solution?
Is there a function that would return the current template as string to pass to has_block()?
The text was updated successfully, but these errors were encountered: