Enqueing dependent scripts/styles only when widget is active
-
Folks,
In the interests of performance and all round good housekeeping, I’d like to minimise the number of scripts/styles that need to be loaded by the browser depending on which widgets I have enabled – so that when at least one instance of a given widget is enabled, its dependent scripts/styles are enqueued as normal with
wp_enqueue_script()
andwp_enqueue_style()
– and when no instances of a given widget are currently enabled, the depdendent scripts/styles are not enqueued and therefore do not appear in the document output.I’ve been looking at some form of mechanism using
is_active_widget()
but I just can’t seem to make it work – i.e.if (is_active_widget('My_Widget_Class')) { wp_enqueue_script(...) wp_enqueue_style(...) }
but it doesn’t seem to work. Note, all my widgets extend the
WP_Widget
class in order to allow them to be used as multi-widgets in 2.8.x (if that makes any difference).Is there a recommended approach to this – or maybe I’m in the right ball-park, but just doing it wrong?
- The topic ‘Enqueing dependent scripts/styles only when widget is active’ is closed to new replies.