0

I've got a conflict with another plugin which is blocking the JS in my custom plugin with the hook:

add_action('wp_enqueue_scripts', '<FOREIGN-PLUGIN>');

Is there a way to enqueue my JS before all other plugins?

I tried already to set priority like

add_action('wp_enqueue_scripts', '<FOREIGN-PLUGIN>', 1);
add_action('wp_enqueue_scripts', '<MY-PLUGIN>', 2);

but it doesn't work. Any ideas?

2
  • Have you tried remove_action() to remove the other plugin's action, add yours, then add back the other plugin's action?
    – Sally CJ
    Oct 16, 2019 at 2:07
  • your plugin should be independent of other plugin actions. that will avoid a lot of headaches in future development and customization. But if you insist on doing so, try wp_deregister_script( 'script-handle' ) then enqueue your script, other script. To register script: wp_register_script(....parameters....) and to enqueue script : wp_enqueue_script('script-handle')
    – maverick
    Oct 16, 2019 at 6:48

2 Answers 2

0

Use dependencies. Dependencies are scripts that your script relies upon to be loaded first. The <FOREIGN-PLUGIN> in the example should be the handle that script was enqueued with.

$deps = array('<FOREIGN-PLUGIN>');
wp_enqueue_script( $handle, $src, $deps);

1
  • Not sure why someone downvoted this, as it's the correct answer and the correct way to enqueue scripts or CSS that are reliant upon other scripts/CSS
    – Mat
    Mar 17 at 18:34
-1

If the plugin does not include a priority, then the priority is "10" (the default).

So without changing (editing) the offending plugin, you can use a priority higher than 10 to load after, or lower than 10 to load earlier.

You mentioned that you want yours to load earlier. If that's the case, the priority in your example is backwards. Setting foreign-plugin to 1 means it will load before my-plugin (set to 2). Instead, it should be:

add_action('wp_enqueue_scripts', '<FOREIGN-PLUGIN>', 2);
add_action('wp_enqueue_scripts', '<MY-PLUGIN>', 1);

Then "<MY-PLUGIN>" will load earlier than "<FOREIGN-PLUGIN>". But as I mentioned, if it's already in another plugin, you shouldn't edit that and the default prior, so your enqueue should simply be:

add_action('wp_enqueue_scripts', '<MY-PLUGIN>', 1);

(Or some priority number from 1-9). Then "my-plugin" will enqueue first.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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