I needed a way to filter a page/posts content before it was loaded so I could add scripts to the header if a specific shortcode was present. After much searching I came across this on http://wpengineer.com

Now I need to extend it a bit further and do the same for sidebars. It can be by a particular widget type, shortcode, code snippet or anything else that would work to identify when the script needs to be loaded.

The problem is I can't figure out how to access the sidebars content before the sidebar is loaded (the theme in question will have several sidebars)

Are scripts in header hard requirement for your situation? Scripts at end of page are easier to deal with conditionally and recommended practice for performance.
–
Rarst♦Sep 28 '10 at 8:36

I did try it in wp_footer first which nicely eliminates the need for prefiltering the content, but while the scripts loaded fine, they didn't work. These scripts have to be in wp_head to do what they need to.
–
Ash GSep 28 '10 at 9:12

To load the script in the page where the widget is loaded only, you will have to add the is_active_widget() code, in you widget class. E.g., see the default recent comments widget (wp-includes/default-widgets.php, line 602):

Can that be called BEFORE the widgets are loaded. To be clear this has to take place before the page even loads. My sample code uses the_posts to filter the content on the page, but that doesn't get the sidebars/widgets.
–
Ash GSep 28 '10 at 9:14

Yes, see the example I added to my answer.
–
sorich87Sep 28 '10 at 9:24

Actually I was. wp_enqueue_script doesn't seem to work when applied to wp_head, but wp_print_scripts does. wp_enqueue_script does work though if applied to init like so: add_action( 'init', 'check_widget' );
–
Ash GSep 29 '10 at 4:25

However it loads the scripts on every page of the site, even if the widget is only active in one sidebar. for example if I have a sidebar for blog pages and a another sidebar for other pages. I add the search widget to the blog sidebar and the scripts does load, but it also loads on pages that don't have the blog sidebar. I need to be able to load the script only if the widget is present on that page.
–
Ash GSep 29 '10 at 4:49

Just wanted to share a solution I worked on which allowed me to be a bit more versatile with my implementation. Rather than having the function check for a variety of shortcodes, I modified it to seek out a single shortcode that references a script/style function that needs to be enqueued. This will work for both methods in other classes (such as plugins that may not do this themselves) and in-scope functions. Just drop the below code in functions.php and add the following syntax to any page/post where you want specific CSS & JS.

Thank you for sharing this tip!
It gave me a little headache, because at first it wasn't working. I think there are a couple of mistakes (maybe typos) in the code above has_my_shortcode and I re-written the function as below:

I think there were two main issues: the break wasn't in the scope of the if statement, and that caused the loop to always break at the first iteration. The other problem was more subtle and difficult to discover. The code above doesn't work if the shortcode is the very first thing written in a post. I had to use the === operator to solve this.

Anyway, thank you so much for sharing this technique, it helped a lot.