You should solve your problem while moving toward a best practice while you’re at it by using wp_enqueue_script()
. This will likely solve your issues because it’s domain agnostic.
Start by moving your JS file(s) to a /js/
folder in your [child] theme or custom plugin. Then load your script like this… (snippets copied and adpated from Codex page).
Theme
Goes into functions.php
:
/**
* Proper way to enqueue script from theme
*/
function theme_name_scripts() {
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/my-file.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
Child Theme
Goes into functions.php
:
/**
* Proper way to enqueue script from child theme
*/
function theme_name_scripts() {
wp_enqueue_script( 'script-name', get_stylesheet_directory_uri() . '/js/my-file.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
Plugin
Goes into a plugin file. For this, there’d probably only be one.
/**
* Proper way to enqueue scripts from a plugin
*/
function theme_name_scripts() {
wp_enqueue_script( 'script-name', plugins_url( '/js/my-file.js', __FILE__ ), array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
This will then work on an domain so long as the file is there.