Check for Gutenberg

This snippet is useful when you are updating an older plugin and don’t want to break backwards compatibility for users on older versions of WordPress. This allows you to conditionally include Gutenberg blocks only if the appropriate Gutenberg core files are available.

<?php
/*******************************************************************
* Checks to see if Gutenberg is set up on the site before attempting to load blocks
*********************************************************************/
function myplugin_setup_blocks() {
   if(function_exists('register_block_type')) {
       require_once( plugin_dir_path( __FILE__ ) . 'blocks/my-block-slug.php');
   }
}
add_action( 'plugins_loaded', 'myplugin_setup_blocks' );

In this example, blocks/my-block-slug.php is your block’s (or blocks’, if multiple) PHP loader file.

Above: Shiny Code ( https://github.com/cedaro/shiny-code )

Leave a Reply

Your email address will not be published. Required fields are marked *