This is the manual page and this is the code.
But I’m still struggling to understand how to use this function. Perhaps I am using it right, but something I can’t think of is wrong. But more surely, I need to know what this means:
-
callback
– I assumed this was the name of the Widget class. Am I right? -
widget id
, what is this? -
id base
, what is it? -
Whether to check in
wp_inactive_widgets
. What iswp_inactive_widgets
?
I really feel like the manual isn’t explaining these things.
Here’s something that I put together which does not work:
function cbs_register_widgets() {
register_widget( 'cbs_map_widget' );
}
add_action( 'widgets_init', 'cbs_register_widgets' );
function cbs_enqueue_resources() {
if ( is_active_widget( 'cbs_map_widget' ) ) {
wp_enqueue_script( 'fancybox-js', plugins_url( '/fancybox/jquery.fancybox-1.3.4.pack.js', __FILE__), array( 'jquery' ), '1.3.4' );
wp_enqueue_style( 'fancybox-css', plugins_url( '/fancybox/jquery.fancybox-1.3.4.js', __FILE__ ), '', '1.3.4' );
}
wp_enqueue_script( 'cbs-widgets-js', plugins_url( '/js/cbs-widgets.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_style( 'cbs-widgets-css', plugins_url( '/css/style.css', __FILE__ ) );
}
add_action( 'template_redirect', 'cbs_enqueue_resources' );
The enqueueing in itself works, it is the is_active_widget
that doesn’t work. The widget itself works too, and displays correctly. Just the fancybox js that’s missing.
Here’s my implementation of one of the answers I got below, it does not work either:
class cbs_map_widget extends WP_Widget
{
function cbs_map_widget()
{
$ops = array('classname' => 'cbs-map-widget', 'description' => 'Visar en karta över Chalmers område där lokaler som används av Sällskapet har markerats.');
$this->WP_Widget('cbs-map-widget', 'CBS kartwidget', $ops);
}
function widget($args, $instance)
{
extract($args);
echo $before_widget;
echo '<div class="overline">text</div>';
echo '<a href="#" class="cbs-map-widget"><img src="' . plugins_url( '/images/map.png', __FILE__ ) . '" title="" /></a>';
echo $after_widget;
add_action('wp_enqueue_scripts', array(&$this, 'js'));
}
function js()
{
if ( is_active_widget(false, false, $this->id_base, true) ) {
wp_enqueue_script( 'fancybox-js', plugins_url( '/fancybox/jquery.fancybox-1.3.4.pack.js', __FILE__), array( 'jquery' ), '1.3.4' );
wp_enqueue_style( 'fancybox-css', plugins_url( '/fancybox/jquery.fancybox-1.3.4.js', __FILE__ ), '', '1.3.4' );
}
}
}