4

I'm enqueuing scripts with dependency in functions.php file like:

wp_enqueue_script('ads', get_template_directory_uri() . '/js/ads.js', array( ), '1.6', true); wp_enqueue_script('banners', get_template_directory_uri() . '/js/banners.js', array( 'ads-REKLAMY' ), $timestamp = filemtime( plugin_dir_path( __FILE__ ) . '/js/banners.js' ), true); 

banners.js is a script which uses imported arrays from ads.js. I need to update ads.js file automatically whenever I make changes there.

For now I use version parameter like "1.6". So I need to manually change it to, for example, "1.7" and change import line in banners.js file.

Of course banners.js updates automatically with timestamp. Is there a way to do that with dependancy file as well?

And do I do that with import in file using dependency? Basically I need ads.js, the dependency file with exported arrays, to update automatically whenever I make changes there. Any solution will do except Webpack.

7
  • 1
    I'm trying to understand your question. For example, why did you use dependency as array( 'ads-REKLAMY' ), why is it not array( 'ads' )? Is there another file with handle ads-REKLAMY?
    – Fayaz
    CommentedFeb 13, 2023 at 10:21
  • I've edited the question to the best of my understanding, and answered the question accordingly. I think you are trying to change the dependency handle name, like ads-REKLAMY to get the change from ads.js. That is not necessary. Please check my answer and let me know if it works for you.
    – Fayaz
    CommentedFeb 13, 2023 at 11:24
  • Yea it should be array( 'ads' ) i was just changing names a bit and forgot to change that one too and yea you understood my problem correctly but what about import in banners.js file should it be just import { selectADS, sidebarADS, mobileADS } from "./ads.js"; ?
    – FilipD
    CommentedFeb 13, 2023 at 12:57
  • Scripts added with wp_enqueue_script cannot be used as a ES6 module by default. WordPress doesn't support that. I recommend you add selectADS, sidebarADS, mobileADS as global JavaScript variables in ads.js, and then access them from banners.js normally. If you must use them as modules (with import statement), then there is workaround for that, like this link explains.
    – Fayaz
    CommentedFeb 13, 2023 at 16:34
  • Ive been using them as modules exactly like that solution explains but now there are problems with import, so can I do it without importing them? selectADS, sidebarADS, mobileADS are functions not variables and when I try to use them in another file without import it doesnt work even tho file with them ads.js is loaded first.
    – FilipD
    CommentedFeb 14, 2023 at 10:10

1 Answer 1

5

If I understood your question correctly, you have two JavaScript files: ads.js and banner.js.

You want the site to load the updated version of both of these files, whenever any of these files are changed.

If this understanding is correct, then you may do the following:

function my_custom_theme_scripts() { $ads_version = filemtime( get_stylesheet_directory() . '/js/ads.js' ); $banners_version = filemtime( get_stylesheet_directory() . '/js/banners.js' ); wp_enqueue_script( 'ads', get_stylesheet_directory_uri() . '/js/ads.js', array(), $ads_version, true ); wp_enqueue_script( 'banners', get_stylesheet_directory_uri() . '/js/banners.js', array( 'ads' ), $banners_version, true ); } add_action( 'wp_enqueue_scripts', 'my_custom_theme_scripts' ); 

This way, whenever any of the files are changed, only the updated versions will be loaded in the frontend. When ads.js is changed, but banners.js is unchanged, the same old banners.js will still get the new ads.js, because the new ads.js will be loaded by the $ads_version timestamp. There's no need to change the dependency handle.

Note: I've used get_stylesheet_directory_uri() and get_stylesheet_directory for consistency (for the currently active theme).

Mixing up get_template_directory_uri() and plugin_dir_path() will work in theory, but that sort of inconsistency may break your code in future. plugin_dir_path() is supposed to be used with plugins, and get_template_directory_uri() for the parent theme.


Also, I've added:

add_action( 'wp_enqueue_scripts', 'my_custom_theme_scripts' );

Just in case you are missing the wp_enqueue_scripts action hook. The code will not work properly without it.

Of course, if you are already using the action hook properly, like:

add_action( 'wp_enqueue_scripts', '<function-that-enqueues-scripts>' );

then please don't add it again.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.