Hey guys so I'm trying to enqueue a javascript with filemtime but every time I do it the custom js script I wrote doesn't seem to enqueue.
function add_js_scripts(){
wp_register_script('lib',
get_template_directory_uri().'/js/lib.js',
array(),
filemtime(get_template_directory() . '/js/lib.js'),
null,
'all'
);
wp_enqueue_script('lib');
}
add_action('wp_enqueue_scripts','add_js_scripts');
The problem is now the lib.js file isnt showing up at all on the website. There are no php errors and if i remove the filemtime
argument it shows up. Ive been version controlling it for development purposes.
Can someone tell me firstly if there is a better way to enqueue the script and if there is a way to enqueue it correctly with filemtime.
Also note i enqueued a style sheet successfully this way:
function add_css_scripts() {
wp_register_style( 'style',
get_template_directory_uri() . '/style/style.css',
array(),
filemtime(get_template_directory() . '/style/style.css'),
null,
'all'
);
wp_enqueue_style('style');
}
add_action( 'wp_enqueue_scripts', 'add_css_scripts' );
Hey guys so I'm trying to enqueue a javascript with filemtime but every time I do it the custom js script I wrote doesn't seem to enqueue.
function add_js_scripts(){
wp_register_script('lib',
get_template_directory_uri().'/js/lib.js',
array(),
filemtime(get_template_directory() . '/js/lib.js'),
null,
'all'
);
wp_enqueue_script('lib');
}
add_action('wp_enqueue_scripts','add_js_scripts');
The problem is now the lib.js file isnt showing up at all on the website. There are no php errors and if i remove the filemtime
argument it shows up. Ive been version controlling it for development purposes.
Can someone tell me firstly if there is a better way to enqueue the script and if there is a way to enqueue it correctly with filemtime.
Also note i enqueued a style sheet successfully this way:
function add_css_scripts() {
wp_register_style( 'style',
get_template_directory_uri() . '/style/style.css',
array(),
filemtime(get_template_directory() . '/style/style.css'),
null,
'all'
);
wp_enqueue_style('style');
}
add_action( 'wp_enqueue_scripts', 'add_css_scripts' );
wp_register_script() function is accepting the 5 parameters. You are passing the 6 parameter. So replace the
wp_register_script(
'lib',
get_template_directory_uri().'/js/lib.js',
array(),
filemtime(get_template_directory() . '/js/lib.js'),
null,
'all'
);
WITH
wp_register_script(
'lib',
get_template_directory_uri().'/js/lib.js',
array(),
filemtime(get_template_directory() . '/js/lib.js'),
true
);
So it seems that sometimes for some reason certain scripts dont seem to enqueue when you register the script then enqueue it when using filemtime in the parameter:
function add_js_scripts(){
wp_register_script('lib',
get_template_directory_uri().'/js/lib.js',
array(),
filemtime(get_template_directory() . '/js/lib.js'),
true);
wp_enqueue_script('lib');
}
add_action('wp_enqueue_scripts','add_js_scripts');
To solve this you can try and enqueue the script in one sub function like this:
wp_enqueue_script('lib',
get_template_directory_uri().'/js/lib.js', array(),
filemtime(get_template_directory() . '/js/lib.js'),
true);
This seemed to solve the problem of the script not enqueueing for me. I jsut wanna thank Chinmoy Kumar Paul for guiding me in the right direction.