Discussion:
Remove wp_enqueue_style called mid-page
Dino Termini
2014-03-26 15:58:07 UTC
Permalink
Hi all,

I am using a plugin that does a wp_enqueue_style mid-page, instead of
using the appropriate hook. This causes the CSS to be added to the
footer, which I would like to avoid.

Aside from contacting the plugin author, is there a way to dequeue that
style so that I can enqueue it in the header? I would like to avoid
editing the plugin's source code to comment out that line of code ;)

I've tried a bunch of combinations of actions/dequeue/deregister, to no
avail.

Thanks,
Dino.
Andrew Nacin
2014-03-26 16:02:46 UTC
Permalink
If you simply enqueue it in time for the header, it should print in the
header and not print it again in the footer.
Post by Dino Termini
Hi all,
I am using a plugin that does a wp_enqueue_style mid-page, instead of
using the appropriate hook. This causes the CSS to be added to the footer,
which I would like to avoid.
Aside from contacting the plugin author, is there a way to dequeue that
style so that I can enqueue it in the header? I would like to avoid editing
the plugin's source code to comment out that line of code ;)
I've tried a bunch of combinations of actions/dequeue/deregister, to no
avail.
Thanks,
Dino.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Nikola Nikolov
2014-03-26 16:03:30 UTC
Permalink
Hi Dino,

Have you tried simply enqueueing that style? In theory WP shouldn't render
the same style/script twice.

function my_enqueue_plugin_style() {
wp_enqueue_style( 'plugin-style' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_plugin_style' );

If the plugin doesn't register the style all the way until it's enqueued,
then it'd be a bit more tricky, but something like this should work:

function my_dequeue_plugin_style() {
wp_dequeue_style( 'plugin-style' );
}
add_action( 'wp_footer', 'my_dequeue_plugin_style' );

function my_enqueue_plugin_style() {
wp_enqueue_style( 'plugin-style', $url_to_plugin_css_file );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_plugin_style' );

Nikola
Post by Dino Termini
Hi all,
I am using a plugin that does a wp_enqueue_style mid-page, instead of
using the appropriate hook. This causes the CSS to be added to the footer,
which I would like to avoid.
Aside from contacting the plugin author, is there a way to dequeue that
style so that I can enqueue it in the header? I would like to avoid editing
the plugin's source code to comment out that line of code ;)
I've tried a bunch of combinations of actions/dequeue/deregister, to no
avail.
Thanks,
Dino.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dino Termini
2014-03-26 17:39:05 UTC
Permalink
Thank you Nikola and Andrew! It worked like a charm!
Post by Nikola Nikolov
Hi Dino,
Have you tried simply enqueueing that style? In theory WP shouldn't render
the same style/script twice.
function my_enqueue_plugin_style() {
wp_enqueue_style( 'plugin-style' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_plugin_style' );
If the plugin doesn't register the style all the way until it's enqueued,
function my_dequeue_plugin_style() {
wp_dequeue_style( 'plugin-style' );
}
add_action( 'wp_footer', 'my_dequeue_plugin_style' );
function my_enqueue_plugin_style() {
wp_enqueue_style( 'plugin-style', $url_to_plugin_css_file );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_plugin_style' );
Nikola
Loading...