Created
          August 26, 2022 13:49 
        
      - 
      
 - 
        
Save mwhiteley16/53cc20c93c17bdbab2fa91fcee371e0b to your computer and use it in GitHub Desktop.  
    Register multiple scripts with block.json
  
        
  
    
      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 | |
| /** | |
| * Blocks setup | |
| * | |
| * @link https://www.advancedcustomfields.com/resources/options-page/ | |
| */ | |
| namespace WhiteleyDesigns\Blocks; | |
| /** | |
| * Load Blocks | |
| */ | |
| function load_blocks() { | |
| $blocks = scandir( get_template_directory() . '/blocks/' ); | |
| $blocks = array_values( array_diff( $blocks, array( 'acf-blocks' ) ) ); | |
| if ( ! empty( $blocks ) ) { | |
| foreach( $blocks as $block ) { | |
| // regsiter block scripts if they exist (must register script before block) | |
| if ( file_exists( get_template_directory() . '/blocks/' . $block . '/block.js' ) ) { | |
| $deps = [ | |
| 'acf', | |
| 'jquery' | |
| ]; | |
| if ( $block == 'slideshow' ) { | |
| $deps[] = 'wd-flickity'; | |
| } | |
| wp_register_script( | |
| 'block-' . $block . '-js', | |
| get_stylesheet_directory_uri() . '/blocks/' . $block . '/block.js', | |
| $deps, | |
| WD_THEME_VERSION, | |
| false | |
| ); | |
| } | |
| // register the block | |
| register_block_type( get_template_directory() . '/blocks/' . $block . '/block.json' ); | |
| } | |
| } | |
| } | |
| add_action( 'init', __NAMESPACE__ . '\load_blocks', 5 ); | 
  
    
      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
    
  
  
    
  | /** | |
| * Frontend scripts & styles | |
| */ | |
| function wd_global_enqueues() { | |
| // flickity | |
| wp_register_script( | |
| 'wd-flickity', | |
| get_stylesheet_directory_uri() . '/assets/js/src/flickity.pkgd.min.js', | |
| [], | |
| null | |
| ); | |
| } | |
| add_action( 'wp_enqueue_scripts', 'wd_global_enqueues' ); | |
| /** | |
| * Block editor scripts | |
| */ | |
| function wd_admin_enqueues() { | |
| // flickity - only needed if using flickity outside of custom ACF blocks | |
| wp_register_script( | |
| 'wd-flickity', | |
| get_stylesheet_directory_uri() . '/assets/js/src/flickity.pkgd.min.js', | |
| [], | |
| null | |
| ); | |
| } | |
| add_action( 'enqueue_block_editor_assets', 'wd_admin_enqueues' ); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment