Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tmtrademark/1172287 to your computer and use it in GitHub Desktop.
Save tmtrademark/1172287 to your computer and use it in GitHub Desktop.
Updtaed for 3.3, thanks xavisys || WidgetWithBehaviorScript: Template for a WordPress widget which enqueues an accompanying behavior script; script only output if widget is rendered.
<?php
class WidgetWithBehaviorScript extends WP_Widget {
function __construct() {
parent::__construct(__CLASS__, 'Widget with Accompanying Behavior Script', array(
'classname' => __CLASS__,
'description' => "This WordPress widget serves as a pattern for how to enqueue a script only if the widget is actually rendered."
));
function widget($args, $instance) {
// Enqueue the .js script which accompanies the widget
wp_enqueue_script(
__CLASS__,
trailingslashit(get_template_directory_uri()) . __CLASS__ . ".js",
array('jquery'), // Whatever you want
filemtime(trailingslashit(TEMPLATEPATH) . __CLASS__ . '.js'),
true // Must be true (in_footer)
);
extract($args, EXTR_SKIP);
global $Shopp;
echo $before_widget;
echo $before_title;
# ...
echo $after_title;
# ...
echo $after_widget;
}
function update($new_instance, $old_instance) {}
function form($instance){}
}
?>
@GaryJones
Copy link

Is there a similar trick, when handling an accompanying .css file reference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment