Support

Account

Home Forums Gutenberg Add script with ACF fields to footer within custom block Reply To: Add script with ACF fields to footer within custom block

  • No prob, but I just tried something that you may be able to advise on.

    When I register the block in functions.php, I can use that wp_add_inline_script to execute code in the back end. But ACF doesn’t work in functions.php. Probably because it doesn’t know which block (or at least post) to pull this “slides_per_view” value from.

    I think you need to target the post ID if you use ACF in functions.php, no? Which is why the last section of this doesn’t work as it does in my slider.php file.

    
    acf_register_block_type(array(
          'name'              => 'slider',
          'title'             => __('Slider'),
          'description'       => __('A custom slider block.'),
          'render_template'   => 'inc/blocks/slider/slider.php',
          'category'          => 'formatting',
          'icon'         => 'images-alt2',
          'align'        => 'full',
          'enqueue_assets'   => function () {
            wp_enqueue_script('swiper', 'https://unpkg.com/swiper@8/swiper-bundle.min.js', array(), '1.8.1', true);
    
            $script = ‘var swiper = new Swiper(“.swiper”, 
            {slidesPerView:’.get_field(”slides_per_view”.’});’;
            
            wp_add_inline_script(‘swiper’, $script);
          }
    
    

    No worries if you’re not sure. You’ve helped plenty.