I've added a custom JQuery script to a new JS file "themes\twentytwentyone\js\script_new.js".
And I load this new JS file in theme functions.php file.
function add_custom_script() {
wp_enqueue_script(
'my_script',
get_stylesheet_directory_uri() .'/js/script_new.js',
array( 'jquery' ),
'1.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'add_custom_script' );
This works without any issue. But is there a way to achieve this without updating the theme functions.php file? I'm not sure if updating the functions.php file is good practice.
CodePudding user response:
Using child's theme functions.php is the best practice .
But if you don't want to use functions.php then you can call js file through footer.php or header.php too.
Please note i recommend and it is the safest to make child theme for any customisation and use Child theme's functions.php.