Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress can't dequeue script/style that has query

Tags:

wordpress

Not sure if I worded it correctly but basically I wanted to load plugin CSS/JS on pages only that uses the actual plugins.. I have gotten a lot of it done by search thru the plugin files for any handles used in wp_enqueue_script within the plugins and simply wp_dequeue_script them in functions.php

However, there are some enqueues for style that include a .php and not a css file, for example.. in the plugin it enqueues a file

wp_enqueue_style("myrp-stuff", MYRP_PLUGIN_URL . "/myrp-hotlink-css.php");

so I've tried:

wp_dequeue_style('myrp-stuff');
wp_deregister_style('myrp-stuff');

It doesn't work

However, when the page/post is rendered it shows as

<link rel='stylesheet' id='myrp-stuff-css'  href='http://www.modernlogic.co/wp/wp-content/plugins/MyRP/myrp-hotlink-css.php?ver=3.4.2' type='text/css' media='all' />

It addes -css to the id and it refuses to dequeue/deregister and be moved.

I have also tried the following with no luck

wp_dequeue_style('myrp-stuff-css');
wp_deregister_style('myrp-stuff-css');

Any suggestions?

like image 936
CI_Guy Avatar asked Sep 17 '12 19:09

CI_Guy


2 Answers

Scripts and styles can be enqueued in any order and at anytime before wp_print_* actions are triggered. Which can make it difficult to remove them from the queue before output.

To make dequeue work consistently hook into into wp_print_styles or wp_print_scripts with a high priority, as this will remove the scripts and styles just before output.

For instance in your plugin loader code or template's functions.php file you could have a function and action hook like this:

function remove_assets() {
    wp_dequeue_style('myrp-stuff');
    wp_deregister_style('myrp-stuff');
}
add_action( 'wp_print_styles', 'remove_assets', PHP_INT_MAX );

Setting a high priority (third argument to add_action) when hooking into the action will help ensure that the callback remove_assets gets called last, just before scripts/styles are printed.

Note, while this technique is legitimate for removing scripts/styles it should't be used for adding assets. See this Wordpress Core blog post for more info.

like image 123
leepowers Avatar answered Oct 08 '22 06:10

leepowers


Just to be sure, have you placed your code inside a function called by an action like this?:

add_action('wp_enqueue_scripts', 'dequeue_function');
function dequeue_function() {
    wp_dequeue_style( array('myrp-stuff', 'myrp-stuff-css') );
    wp_deregister_style( array('myrp-stuff', 'myrp-stuff-css') );
}
like image 29
Adal Avatar answered Oct 08 '22 04:10

Adal