Home Forums Gutenberg Enqueueing a script when a block is used


Enqueueing a script when a block is used

    • mefusia

    • March 21, 2019 at 2:43 am

    I’ve created a gallery block that needs JS to work, but I only use it on a few pages and don’t want to enqueue a heavy JS file on my whole website. Is there a way to check if a block is used on a page and enqueue a script only when its needed?

  • There isn’t any reason to check, really. When you display the block content just enqueue the script there in the footer. You can event “enqueue” a script multiple times and only the first one will count. As an example, I am currently working on a site where I’m using slick slider. I have set up multiple flexible content layouts that use it. Inside my code that displays the flex layout I enqueue the scripts I need.

    • mefusia

    • March 22, 2019 at 4:03 am

    I’ll do that, thanks for the information 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.