0

I'm developing plugin where I want to load my dependency JS libraries only when the setting is enabled. The most correct way is to register all the scripts and styles I have and then enqueue my main script with dependencies, but I can't figure it out how to load them only when setting is enabled. Here's my current code:

function registerAssets() {
    wp_register_script('main', plugins_url('public/assets/js/script-main.js', $this->pluginFile), array('additionaljs', 'additionaljs2'), $this->version, true);
    wp_register_script('additionaljs', plugins_url('public/assets/js/additionaljs.js', $this->pluginFile), array(), $this->version, true);
    wp_register_script('additionaljs2', plugins_url('public/assets/js/additionaljs2.js', $this->pluginFile), array(), $this->version, true);
} // on action - init

function enqueueAssets() {
    wp_enqueue_script("main");
} // on action - wp_enqueue_scripts

so now I want to make them conditional, so only load dependency additionaljs, if setting is on. So I wrapped register function with if conditional:

if ($this->settings['additionaljs'] == 'on') {
    wp_register_script('additionaljs', plugins_url('public/assets/js/additionaljs.js', $this->pluginFile), array(), $this->version, true);
}

Final code is this:

function registerAssets() {
    wp_register_script('main', plugins_url('public/assets/js/script-main.js', $this->pluginFile), array('additionaljs', 'additionaljs2'), $this->version, true);
    if ($this->settings['additionaljs'] == 'on') {
        wp_register_script('additionaljs', plugins_url('public/assets/js/additionaljs.js', $this->pluginFile), array(), $this->version, true);
    }
    wp_register_script('additionaljs2', plugins_url('public/assets/js/additionaljs2.js', $this->pluginFile), array(), $this->version, true);
} // on action - init

function enqueueAssets() {
    wp_enqueue_script("main");
} // on action - wp_enqueue_scripts

But unfortunately it does not works and results in not loadingmain script at all. I want to load main.js with additionaljs2.js as dependency and only if $this->settings['additionaljs'] is on also load additionaljs.js as dependency..

So how can i do this?

  • That looks reasonable (except you're missing a closing bracket after == 'on'). Can you be more specific about how it's not working - do you get an error (on the page or in the logs), does the script never load even if the setting is set, can you print out the value of $this->settings['additionaljs'] to make sure it's what you expect, etc.? – Rup Jul 14 '19 at 13:49
  • 1
    If your main script works without additionaljs, then additionaljs is not a dependency and shouldn't be registered as such. – Jacob Peattie Jul 14 '19 at 13:58
  • @Rup missing bracket was just typo, so it's not the issue. The script loads and causing no errors, just main script is not loading at all. I want to load main.js with additionaljs2.js as dependency and only if $this->settings['additionaljs'] is on, also load additionaljs.js as dependency. – Akaki Khujadze Jul 14 '19 at 17:04
  • @JacobPeattie main.js includes some code, which initializes additional.js and additionaljs2.js contents. They are JS plugins and I call them in main.js. So main.js will work, but part of it won't work without additional.js or additionaljs2.js. – Akaki Khujadze Jul 14 '19 at 17:06
  • 1
    But you're still declaring additionaljs as a dependency of main: wp_register_script('main', ..., array('additionaljs', 'additionaljs2'), ...); I think Jacob's point is that you shouldn't do that, e.g. by removing additionaljs when $this->settings['additionaljs'] != 'on'. Alternatively you could omit the dependencies altogether and just conditionally wp_enqueue_script the appropriate set based on your condition. – Rup Jul 14 '19 at 21:49

Your Answer

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

Browse other questions tagged or ask your own question.