You can do different things:
- Enqueue the new JS file via
plugin
- Enqueue the new JS file via
functions.php
- Add your JS code into
functions.php
throughwp_header
orwp_footer
For adding new JS files to a wordpress site you should enqueue these files with wp_enqueue_style()
and/or wp_enqueue_script()
.
This is the proper and safest way.
I would go the route and create a simple, small plugin for that, because this is theme independent.
You could also use an hook to wp_header
or wp_footer
. Inside this hook you can write your JS code.
But the proper way is enqueueing these files which will work something like this:
function prfx_frontend_enqueue() {
wp_enqueue_style( 'my-plugin-css', plugins_url('/assets/css/plugin.css', __FILE__) );
wp_enqueue_script( 'my-plugin-js', plugins_url('/assets/js/jquery.my-script-min.js', __FILE__), array('jquery'));
}
add_action('wp_enqueue_scripts', 'prfx_frontend_enqueue');