0

There is a theme my client is using. It enqueues many scripts in the admin area of WordPress. I'd like to dequeue these so that my plugin works properly.

Commenting out this code wp_enqueue_script( "ocmx-jquery", get_template_directory_uri()."/scripts/ocmx.js", array( "jquery" ) ); makes my plugin work properly. So rather than edit their code, I want to add the following to my plugin wp_dequeue_script( 'ocmx-jquery' );. I've added that, and it's as if I never did.

Is there a better way to tell the theme's JS not to load in my plugins settings page?

4
  • possible duplicate of How to solve a conflict between a plugin and a theme?
    – brasofilo
    Oct 7, 2013 at 1:07
  • I tried the solution from that page, and it didn't work. The page still functioned as though I didn't have the code in place.
    – Branndon
    Oct 7, 2013 at 2:30
  • @Branndon You'll have to use the appropriate hooks. admin_head, admin_print_scripts, admin_enqueue_scripts, etc. Please show us in an edit how exactly your tried to get rid of that script. What you should as well do is notifying those guys who developed that theme that it's complete crap! to do that.
    – kaiser
    Oct 26, 2013 at 23:24
  • Make sure you use exactly the same handle as whats used when its enqueued Feb 8, 2015 at 15:40

3 Answers 3

7

wp_enqueue_script() also registers the script. So, you'll also need to de-register it first. This code should work depending on how and where the script is enqueued.

function my_dequeue() {
    wp_deregister_script( 'ocmx-jquery' );
    wp_dequeue_script( 'ocmx-jquery' );
}
add_action( 'admin_enqueue_scripts', 'my_dequeue', 10 );

You might also need to play with the priority of the action.

1
  • 2
    you don't need both, dequeueing the script is enough. you only need to deregister a script if you want to register your own with the same handle.
    – Milo
    Feb 8, 2015 at 5:19
1

You need to dequeue after the other script has been enqueued. That probably means hooking into wp-enqueue_scripts.

add_action(
  'wp_enqueue_scripts',
  function() {
    wp_dequeue_script( 'ocmx-jquery' );
  }
);

Or a version for an older PHP:

function dequeue_ocmx_jquery() {
    wp_dequeue_script( 'ocmx-jquery' );
}
add_action(
  'wp_enqueue_scripts',
  'dequeue_ocmx_jquery'
);
4
  • Tried this, doesn't work. It's at the top of my options page, and now the page won't even load. If I remove this code, the page loads again.
    – Branndon
    Oct 7, 2013 at 2:23
  • This should not really go at the top of your options page. Try the other version
    – s_ha_dum
    Oct 7, 2013 at 2:55
  • Thanks @s_ha_dum the second didn't work either. I didn't want to dequeue their script globally, only on my settings page. That's why I put it in my options page code. If I just put the code in the main admin area, it would stop their code from running anywhere, and I didn't want to do that.
    – Branndon
    Oct 7, 2013 at 16:09
  • 1
    Try admin_enqueue_scripts instead of wp_enqueue_scripts. I didn't realize this was a backend problem. However, depending on where that plugin has its code, you may not be able to get rid of it.
    – s_ha_dum
    Oct 7, 2013 at 16:11
0

http://codex.wordpress.org/Function_Reference/wp_dequeue_script

Have a look especially at the bottom of the page, i think this is what you want : "Admin: admin_enqueue_scripts, admin_print_scripts, admin_print_styles"

1
  • Please explain how to use wp_dequeue_script() and what exactly those hooks do and how to use them. Please also care a bit about the formatting of your question. Hint: The WYSIWYG editor got a (?) help link and something to format links and code segments. Have fun on the site :)
    – kaiser
    Oct 26, 2013 at 23:23

Your Answer

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

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