I finally refused.
I am developing something in WordPress 2.8.4. I am impressed with the ease of the new widget API, which allows you to use WP_Widget extensions and create widgets that are easily accessible with multiple instances. But I ran into a problem.
How can I automatically activate a widget when activating a theme? I tried to use:
add_action('sidebars_widgets', array('sidebar-1', array('uc_tagcloud')));
But without success. the problem is that the New wordpress API creates widget identifiers and automatically adds a unique identifier to the end of the identifier. So I just can’t figure it out. I tried the above solution, but the actual widget id, while looking at the source code, always displays uc_tagcloud-2 or 3 or 4..etc etc. A new instance each time a widget is added.
I would be grateful for any thought, I thought deeply about it and searched the Internet for hours. So this is my last chance.
Basically, I don't want users to drag and drop them manually.
, . , . , .. : , (WP_Widget new Widgets API)
:
<?php
class uc_tagcloud extends WP_Widget {
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);
}
function widget($args, $instance) {
extract($args);
$title = esc_attr($instance['title']);
$num = intval($instance['num']);
echo $before_widget.$before_title.$title.$after_title;
?>
<?php wp_tag_cloud('smallest=9&largest=22&number='.$num); ?>
<?php
echo $after_widget;
}
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;
}
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
}
}
add_action('widgets_init', 'uc_tagcloud_init');
function uc_tagcloud_init() {
register_widget('uc_tagcloud');
}
?>
, API . , . ?