Created
June 10, 2021 06:55
-
-
Save codetot/0b341ece6f9ad43dfae846dd1afa3fb0 to your computer and use it in GitHub Desktop.
Check if WP Gutenberg block exists to load css/js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'enqueue_block_assets', 'example_block_assets'); | |
function example_block_assets() { | |
global $post; | |
$has_your_own_blocks = false; | |
if ( ! is_admin() && is_singular() ) { | |
$wp_post = get_post( $post ); | |
if ( $wp_post instanceof WP_Post ) { | |
$has_your_own_blocks = ! empty( | |
array_filter( | |
array( | |
false !== strpos( $wp_post->post_content, '<!-- wp:your-blocks-prefix/' ), | |
has_block( 'core/block', $wp_post ), | |
has_block( 'core/button', $wp_post ), | |
has_block( 'core/cover', $wp_post ), | |
has_block( 'core/heading', $wp_post ), | |
has_block( 'core/image', $wp_post ), | |
has_block( 'core/gallery', $wp_post ), | |
has_block( 'core/list', $wp_post ), | |
has_block( 'core/paragraph', $wp_post ), | |
has_block( 'core/pullquote', $wp_post ), | |
has_block( 'core/quote', $wp_post ), | |
) | |
) | |
); | |
} | |
} | |
if ( ! $has_your_own_blocks && ! $this->is_page_gutenberg() ) { | |
return; | |
} | |
// Styles. | |
$name = 'your-own-style-style'; | |
$filepath = 'dist/' . $name; | |
$asset_file = $this->get_asset_file( $filepath ); | |
$rtl = ! is_rtl() ? '' : '-rtl'; | |
wp_enqueue_style( | |
'your-own-plugin-style-name', | |
YOUR_PLUGIN_URI . $filepath . '.css', | |
array(), | |
YOUR_PLUGIN_VERSION | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment