Created
          August 26, 2022 18:42 
        
      - 
      
 - 
        
Save mwhiteley16/4b6330202d3edaadad7b66641dd89295 to your computer and use it in GitHub Desktop.  
    Conditionally load acf scripts
  
        
  
    
      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( '_base', '.DS_Store' ) ) ); | |
| if ( ! empty( $blocks ) ) { | |
| foreach( $blocks as $block ) { | |
| $editor_script = []; | |
| // 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 | |
| ); | |
| $editor_script = [ | |
| 'editor_script' => 'block-' . $block . '-js' | |
| ]; | |
| } | |
| // register the block | |
| register_block_type( | |
| get_template_directory() . '/blocks/' . $block . '/block.json', | |
| $editor_script | |
| ); | |
| } | |
| } | |
| } | |
| add_action( 'init', __NAMESPACE__ . '\load_blocks', 5 ); | |
| /** | |
| * Load frontend scripts only if block exists on page | |
| */ | |
| function load_frontend_scripts() { | |
| if ( is_singular() && function_exists( 'load_frontend_block_scripts' ) ) { | |
| load_frontend_block_scripts(); | |
| } | |
| } | |
| add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\load_frontend_scripts' ); | 
  
    
      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 | |
| /** | |
| * Load frontend scripts helper | |
| */ | |
| function load_frontend_block_scripts() { | |
| $blocks = scandir( get_template_directory() . '/blocks/' ); | |
| $blocks = array_values( array_diff( $blocks, array( '_base', '.DS_Store' ) ) ); | |
| 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' ) && has_block( 'wd/acf-' . $block ) ) { | |
| wp_enqueue_script( 'block-' . $block . '-js' ); | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment