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!")
})
3 comments
D Ee
Hi, what's the pro and cons using this vs using script plugins like wpcodebox ?
Sridhar Katakam
WPCodeBox does not yet write code to the file system which will always be faster than loading code from the database.
D Ee
wow. that's new. thanks