Hello,
I gave up on this finally.
I am developing something on WordPress 2.8.4. I am impressed by the ease of new widgets API which allows you to use extends WP_Widget and create widgets that have multiple instances easily. But I am facing a problem.
How can I auto-activate the widget on theme activation? I've tried to use:
add_action('sidebars_widgets', array('sidebar-1', array('uc_tagcloud')));
But with no success. the problem is that New wordpress API creates widget ids and adds a unique id to the end of ID automatically. So I just can't get the hang of it. I've tried the above solution but the actual widget ID by viewing source code always displays uc_tagcloud-2 or 3 or 4..etc etc.. a new instance every time i add the widget.
I would appreciate any thought, I've deeply thought about it and searched the internet for hours now. So this is my last chance.
I basically do not want users to drag and enable them manually.
Below is a sample widget I've developed. It works fine if i drag and put it in relevant sidebar. But I do not know how .. my question is : how can I activate it automatically into specific sidebar without draggin it manually (WP_Widget new Widgets API)
This is code of widget:
<?php
/**********************************************************************
Widget: Tag Cloud List
**********************************************************************/
class uc_tagcloud extends WP_Widget {
// Constructor
function uc_tagcloud() {
$widget_ops = array('description' => __('A list of your blog tags for your sidebar','ucms'));
$this->WP_Widget('uc_tagcloud', __('ultimaCMS - Tag Cloud','ucms'), $widget_ops);
}
// Display Widget
function widget($args, $instance) {
extract($args);
$title = esc_attr($instance['title']);
$num = intval($instance['num']);
echo $before_widget.$before_title.$title.$after_title;
// Display widget content
?>
<?php wp_tag_cloud('smallest=9&largest=22&number='.$num); ?>
<?php
echo $after_widget;
}
// When Widget Control Form Is Posted
function update($new_instance, $old_instance) {
if (!isset($new_instance['submit'])) {
return false;
}
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['num'] = intval($new_instance['num']);
return $instance;
}
// DIsplay Widget Control Form
function form($instance) {
global $wpdb;
$instance = wp_parse_args((array) $instance, array('title' => __('Tag Cloud','ucms'), 'num' => 100));
$title = esc_attr($instance['title']);
$num = intval($instance['num']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','ucms'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('num'); ?>"><?php _e('Number of tags:','ucms'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('num'); ?>" name="<?php echo $this->get_field_name('num'); ?>" type="text" value="<?php echo $num; ?>" />
<br /><small>Enter 0 to display all tags</small>
</p>
<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php
}
}
### Initiate widget
add_action('widgets_init', 'uc_tagcloud_init');
function uc_tagcloud_init() {
register_widget('uc_tagcloud');
}
?>
It's very simple, the new API i love it. But I just can't get how to auto activate an instance of the widget on specific sidebar. Any help?