Add scripts when widget active and loaded only

Hints :

function widgets()
//to enqueue on spesific / active page ( where the widget active )
global $widget_print_my_script;
$widget_print_my_script = true; //just flag to display scripts

global $taxonomyname; //globalize $instance;
$taxonomyname = $instance[‘taxonomyname’];

//load spesific custom script based on
add_action(‘wp_footer’, array(&$this, ‘print_widget_js’), 100 ); //lower priority order


function print_widget_js() {

global $taxonomyname; //call the instance
// global $widget_print_my_script; // Set this to true in your widget’s ‘widget’ method
//if (!$widget_print_my_script) return;
// wp_print_scripts(‘my_script’);

<script>// <![CDATA[
//print instance here ? cannot using $instance[‘id’];
//need to find out get instance field here
$this->id; //widget ID can be used as element ID
echo $taxonomyname;
//sload print_widget_js here – how insert widget instance here ?

// ]]></script>
<?php }

Comments are closed.