$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - enqueue_script with filemtime javascript not working|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - enqueue_script with filemtime javascript not working

matteradmin9PV0评论

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' );
Share Improve this question edited Feb 1, 2019 at 14:48 Chinmoy Kumar Paul 9436 silver badges7 bronze badges asked Feb 1, 2019 at 14:37 Zayd BhyatZayd Bhyat 892 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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
); 

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.

Post a comment

comment list (0)

  1. No comments so far