How to deploy php code and Javascript in footer using custom blocks
-
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
- The topic ‘How to deploy php code and Javascript in footer using custom blocks’ is closed to new replies.