• Hi I am creating a custom block for use in the Gutenberg editor. I am using a block.json file to point to the right code and everything is working fine. However I need some JavaScript to work in the footer if the block gets called.

    My block.json code looks like this:

    {
      "name": "acf/gallery",
      "title": "Gallery Block",
      "description": "A custom block for pictures.",
      "editorStyle": "file:./gallery.css",
      "script": "splide",
      "category": "formatting",
      "icon": "format-gallery",
      "keywords": ["gallery", "pictures", "images"],
      "supports": {
        "mode": false
      },
      "acf": {
        "mode": "edit",
        "renderTemplate": "gallery.php",
      },
      "align": "full"
    
    }

    and my call in functions looks like this:

    add_action( 'enqueue_block_assets', function(){
    
    	wp_register_script( 'splide', get_template_directory_uri() . '/assets/js/splide/js/splide.min.js', false, null, true );
      
    });

    This works fine, The gallery.php code is working and I can see the link to splide.min.js is working but what I need to do is place some php and javascript in the footer only if the block is in use.

    I know in the footer I can use:

    if(has_block('acf/gallery')){
        //do something
    }

    And this works fine for the post but it doesn’t work if viewing posts in the blog roll. So how do I do that? Thanks

    • This topic was modified 1 year, 7 months ago by cannon303.
    • This topic was modified 1 year, 7 months ago by cannon303.
    • This topic was modified 1 year, 7 months ago by cannon303.
Viewing 1 replies (of 1 total)
  • Hi @cannon303. This should be possible using get_posts to grab all posts matching the page’s current query and then running has_block against each post.

    $posts = get_posts();
    foreach ($posts as $post) {
        if ( has_block( 'acf/gallery', $post ) {
            // Do something
        }
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to deploy php code and Javascript in footer using custom blocks’ is closed to new replies.