0

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' );
2

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
); 
15
  • Unfortunately this didn't seem to work. The lib.js file doesnt enqueue at all if I dont include the null parameter.
    – Zayd Bhyat
    Feb 1 '19 at 14:55
  • I made mistake. last parameter would be true. Feb 1 '19 at 14:57
  • ok ill try this
    – Zayd Bhyat
    Feb 1 '19 at 15:00
  • if you add true, your JS file will add at footer (before the closing </body> tag). If you add false or nothing, JS will add to <head> tag. Feb 1 '19 at 15:03
  • Ok that didnt seem to work either.
    – Zayd Bhyat
    Feb 1 '19 at 15:03
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.