I have a site with multiple sidebars and I am making a widget that has some javascript in it that will only be on some pages. I am trying to set it up so that, if the widget is active on a page (through the sidebar that is loading in the template), it will add the javascript to the header. My current code is:
<?php
function TCWidget(){
$widget_ops = array('classname' => 'widget_tc', 'description' => __( "My widget") );
$control_ops = array('width' => 200, 'height' => 150);
$this->WP_Widget('tcwidget', __('TC Widget'), $widget_ops, $control_ops);
if ( is_active_widget() )
add_action('wp_head', 'tc_widget_header');
}
function tc_widget_header() { ?>
<script type="text/javascript" language="javascript" src="myscript.js"></script>
<?php
}
The problem is the script never appears in the header, whether the widget is in the sidebar or not.
Please help, as this is driving me insane!!!
Thanks!