/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Skeleton from https://gist.github.com/392765 class WidgetWithBehaviorScript extends WP_Widget { function __construct() { 'classname' => __CLASS__, 'description' => "This WordPress widget serves as a pattern for how to enqueue a script only if the widget is actually rendered." )); // Enqueue the .js script which accompanies the widget wp_enqueue_script( __CLASS__, trailingslashit(get_template_directory_uri()) . __CLASS__ . ".js", true // Must be true (in_footer) ); // Schedule an action to remove the enqueued script; this action is // removed if the widget is actually rendered (if the widget() is called) } /** * Remove the enqueued script */ function remove_enqueued_script(){ wp_deregister_script(__CLASS__); } function widget($args, $instance) { // Abort the scheduled removal of the enqueued script because the widget is being rendered global $Shopp; echo $before_widget; echo $before_title; # ... echo $after_title; # ... echo $after_widget; } function update($new_instance, $old_instance) {} function form($instance){} }