11th Nov '22
/
5 comments

Enqueueing a JavaScript File in Bricks

Enqueueing a JavaScript File in Bricks

Bricks child theme’s functions.php comes with code to enqueue (load) style.css on the front end.

What if you want to load a custom JavaScript file in addition to the CSS file?

Add

wp_enqueue_script( 'bricks-child', get_stylesheet_directory_uri() . '/assets/js/script.js', [], filemtime( get_stylesheet_directory() . '/assets/js/script.js' ), true );

below

wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );

Place your custom JS inside a file named script.js in your child theme at this location: /assets/js.

Sample URL:

https://bricks.local/wp-content/themes/bricks-child/assets/js/script.js

Here’s a boiler plate template for script.js:

document.addEventListener("DOMContentLoaded", () => {
    console.log("Hello Bricks!")
})
Get access to all 611 Bricks code tutorials with BricksLabs Pro

5 comments

Leave a Reply to D Ee (Cancel Reply)

 

Related Tutorials..

Pro
How to Link to a Specific Tab in Bricks

How to Link to a Specific Tab in Bricks

This Pro tutorial provides the JavaScript code that will automatically switch to the tab when using Nestable Tabs elements based on the "tab" URL parameter…
Categories:
Pro
tsParticles in Bricks

tsParticles in Bricks

Updated on 5 Aug 2023 This Pro tutorial provides the steps to set up tsParticles, a lightweight TypeScript (uses JavaScript) library for creating particles as…
Categories:
McDonald’s Order Promise Analogy

McDonald’s Order Promise Analogy

From the JavaScript course I am doing now: A Promise is like placing an order at a restaurant. You receive a ticket (Promise object) that…
Categories:
Setting Active Tab in Bricks’ Tabs (Nestable) Element

Setting Active Tab in Bricks’ Tabs (Nestable) Element

With Bricks' Nestable tab element, here's how we can set which tab is active by default.
Categories:
Auto-switching Bricks Tabs

Auto-switching Bricks Tabs

How to switch tabs every x seconds when the tabs element is scrolled into view.
Categories:
How to change the text of an element based on breakpoints without creating duplicated content

How to change the text of an element based on breakpoints without creating duplicated content

In this tutorial, we'll learn how to dynamically change the text of a basic text element based on the viewport width in Bricks. Introduction Sometimes…
Categories:
Opening RSS Block’s Links in New Tabs

Opening RSS Block’s Links in New Tabs

Looking to have links output by RSS Block (in the block editor) in new tab/window when using Bricks? Add this in your Bricks template/Page at…
Categories:
Pro
Switching Tabs on Hover in Bricks

Switching Tabs on Hover in Bricks

This Pro tutorial shows how to make the tabs of Tabs (Nestable) elements active on hover in Bricks. Step 1 Add one or more Tabs…
Categories: