I know this question has been asked in the past but the accepted answer is incorrect. is_active_widget
returns true if the widget is registered on the site. I.e., it was dragged to the right in Appearance > Widgets
in the admin or added via the Customizer.
What I'm looking to do is load a script only if the widget is actually present in the sidebar on specific page. Case in point: Template A has a sidebar with a recent posts widget and Template B has a different sidebar with a recent comments widget. I want the script to load on the front end only if the recent comments widget is on the page.
is_active_widget('','','widget_recent_comments')
returns true on every page load regardless if the widget is actually loaded, since that widget is registered in the backend.
I know you can call wp_enqueue_script from the widget()
method, but that loads in the footer. I need it to load in the header.
wp_get_sidebars_widgets()
to check in which sidebars the widget you are targeting is loaded, capture the full html using output buffering and then match the sidebar id in the full html output, but it feels a bit hacky.