5 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
7 $wp_registered_sidebars = array();
8 $wp_registered_widgets = array();
9 $wp_registered_widget_controls = array();
11 /* Template tags & API functions */
13 function register_sidebars($number = 1, $args = array()) {
14 global $wp_registered_sidebars;
15 $number = (int) $number;
17 if ( is_string($args) )
18 parse_str($args, $args);
20 for ( $i=1; $i <= $number; $i++ ) {
24 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
26 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
29 if (isset($args['id'])) {
30 $_args['id'] = $args['id'];
32 $n = count($wp_registered_sidebars);
35 $_args['id'] = "sidebar-$n";
36 } while (isset($wp_registered_sidebars[$_args['id']]));
39 register_sidebar($_args);
43 function register_sidebar($args = array()) {
44 global $wp_registered_sidebars;
46 if ( is_string($args) )
47 parse_str($args, $args);
49 $i = count($wp_registered_sidebars) + 1;
52 'name' => sprintf(__('Sidebar %d'), $i ),
54 'before_widget' => '<li id="%1$s" class="widget %2$s">',
55 'after_widget' => "</li>\n",
56 'before_title' => '<h2 class="widgettitle">',
57 'after_title' => "</h2>\n",
60 $sidebar = array_merge($defaults, (array) $args);
62 $wp_registered_sidebars[$sidebar['id']] = $sidebar;
64 return $sidebar['id'];
67 function unregister_sidebar( $name ) {
68 global $wp_registered_sidebars;
70 if ( isset( $wp_registered_sidebars[$name] ) )
71 unset( $wp_registered_sidebars[$name] );
74 function register_sidebar_widget($name, $output_callback, $classname = '') {
76 if ( is_array($name) ) {
77 if ( count($name) == 3 )
78 $name = sprintf($name[0], $name[2]);
83 $id = sanitize_title($name);
85 if ( !empty($classname) && is_string($classname) )
86 $options['classname'] = $classname;
87 $params = array_slice(func_get_args(), 2);
88 $args = array($id, $name, $output_callback, $options);
89 if ( !empty($params) )
90 $args = array_merge($args, $params);
92 call_user_func_array('wp_register_sidebar_widget', $args);
95 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
96 global $wp_registered_widgets;
98 $id = strtolower($id);
100 if ( empty($output_callback) ) {
101 unset($wp_registered_widgets[$id]);
105 $defaults = array('classname' => $output_callback);
106 $options = wp_parse_args($options, $defaults);
110 'callback' => $output_callback,
111 'params' => array_slice(func_get_args(), 4)
113 $widget = array_merge($widget, $options);
115 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
116 $wp_registered_widgets[$id] = $widget;
119 function wp_widget_description( $id ) {
120 if ( !is_scalar($id) )
123 global $wp_registered_widgets;
125 if ( isset($wp_registered_widgets[$id]['description']) )
126 return wp_specialchars( $wp_registered_widgets[$id]['description'] );
129 function unregister_sidebar_widget($id) {
130 return wp_unregister_sidebar_widget($id);
133 function wp_unregister_sidebar_widget($id) {
134 wp_register_sidebar_widget($id, '', '');
135 wp_unregister_widget_control($id);
138 function register_widget_control($name, $control_callback, $width = '', $height = '') {
140 if ( is_array($name) ) {
141 if ( count($name) == 3 )
142 $name = sprintf($name[0], $name[2]);
147 $id = sanitize_title($name);
149 if ( !empty($width) )
150 $options['width'] = $width;
151 if ( !empty($height) )
152 $options['height'] = $height;
153 $params = array_slice(func_get_args(), 4);
154 $args = array($id, $name, $control_callback, $options);
155 if ( !empty($params) )
156 $args = array_merge($args, $params);
158 call_user_func_array('wp_register_widget_control', $args);
161 /* $options: height, width, id_base
163 * width: width of fully expanded control form. Try hard to use the default width.
164 * id_base: for multi-widgets (widgets which allow multiple instances such as the text widget), an id_base must be provided.
165 * the widget id will ennd up looking like {$id_base}-{$unique_number}
167 function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
168 global $wp_registered_widget_controls;
170 $id = strtolower($id);
172 if ( empty($control_callback) ) {
173 unset($wp_registered_widget_controls[$id]);
177 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
180 $defaults = array('width' => 250, 'height' => 200 ); // height is never used
181 $options = wp_parse_args($options, $defaults);
182 $options['width'] = (int) $options['width'];
183 $options['height'] = (int) $options['height'];
188 'callback' => $control_callback,
189 'params' => array_slice(func_get_args(), 4)
191 $widget = array_merge($widget, $options);
193 $wp_registered_widget_controls[$id] = $widget;
196 function unregister_widget_control($id) {
197 return wp_unregister_widget_control($id);
200 function wp_unregister_widget_control($id) {
201 return wp_register_widget_control($id, '', '');
204 function dynamic_sidebar($index = 1) {
205 global $wp_registered_sidebars, $wp_registered_widgets;
207 if ( is_int($index) ) {
208 $index = "sidebar-$index";
210 $index = sanitize_title($index);
211 foreach ( $wp_registered_sidebars as $key => $value ) {
212 if ( sanitize_title($value['name']) == $index ) {
219 $sidebars_widgets = wp_get_sidebars_widgets();
221 if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
224 $sidebar = $wp_registered_sidebars[$index];
227 foreach ( $sidebars_widgets[$index] as $id ) {
228 $params = array_merge(
229 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
230 (array) $wp_registered_widgets[$id]['params']
233 // Substitute HTML id and class attributes into before_widget
235 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
236 if ( is_string($cn) )
237 $classname_ .= '_' . $cn;
238 elseif ( is_object($cn) )
239 $classname_ .= '_' . get_class($cn);
241 $classname_ = ltrim($classname_, '_');
242 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
244 $params = apply_filters( 'dynamic_sidebar_params', $params );
246 $callback = $wp_registered_widgets[$id]['callback'];
248 if ( is_callable($callback) ) {
249 call_user_func_array($callback, $params);
257 /* @return mixed false if widget is not active or id of sidebar in which the widget is active
259 function is_active_widget($callback, $widget_id = false) {
260 global $wp_registered_widgets;
262 $sidebars_widgets = wp_get_sidebars_widgets(false);
264 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
265 if ( is_array($widgets) ) foreach ( $widgets as $widget )
266 if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
267 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
274 function is_dynamic_sidebar() {
275 global $wp_registered_widgets, $wp_registered_sidebars;
276 $sidebars_widgets = get_option('sidebars_widgets');
277 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
278 if ( count($sidebars_widgets[$index]) ) {
279 foreach ( $sidebars_widgets[$index] as $widget )
280 if ( array_key_exists($widget, $wp_registered_widgets) )
287 /* Internal Functions */
289 function wp_get_sidebars_widgets($update = true) {
290 global $wp_registered_widgets, $wp_registered_sidebars;
292 $sidebars_widgets = get_option('sidebars_widgets');
293 $_sidebars_widgets = array();
295 if ( !isset($sidebars_widgets['array_version']) )
296 $sidebars_widgets['array_version'] = 1;
298 switch ( $sidebars_widgets['array_version'] ) {
300 foreach ( $sidebars_widgets as $index => $sidebar )
301 if ( is_array($sidebar) )
302 foreach ( $sidebar as $i => $name ) {
303 $id = strtolower($name);
304 if ( isset($wp_registered_widgets[$id]) ) {
305 $_sidebars_widgets[$index][$i] = $id;
308 $id = sanitize_title($name);
309 if ( isset($wp_registered_widgets[$id]) ) {
310 $_sidebars_widgets[$index][$i] = $id;
316 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
317 if ( strtolower($widget['name']) == strtolower($name) ) {
318 $_sidebars_widgets[$index][$i] = $widget['id'];
321 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
322 $_sidebars_widgets[$index][$i] = $widget['id'];
331 unset($_sidebars_widgets[$index][$i]);
333 $_sidebars_widgets['array_version'] = 2;
334 $sidebars_widgets = $_sidebars_widgets;
335 unset($_sidebars_widgets);
338 $sidebars = array_keys( $wp_registered_sidebars );
339 if ( !empty( $sidebars ) ) {
340 // Move the known-good ones first
341 foreach ( $sidebars as $id ) {
342 if ( array_key_exists( $id, $sidebars_widgets ) ) {
343 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
344 unset($sidebars_widgets[$id], $sidebars[$id]);
348 // Assign to each unmatched registered sidebar the first available orphan
349 unset( $sidebars_widgets[ 'array_version' ] );
350 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
351 $_sidebars_widgets[ $sidebar ] = $widgets;
353 $_sidebars_widgets['array_version'] = 3;
354 $sidebars_widgets = $_sidebars_widgets;
355 unset($_sidebars_widgets);
359 update_option('sidebars_widgets', $sidebars_widgets);
362 unset($sidebars_widgets['array_version']);
364 return $sidebars_widgets;
367 function wp_set_sidebars_widgets( $sidebars_widgets ) {
368 update_option( 'sidebars_widgets', $sidebars_widgets );
371 function wp_get_widget_defaults() {
372 global $wp_registered_sidebars;
376 foreach ( $wp_registered_sidebars as $index => $sidebar )
377 $defaults[$index] = array();
382 /* Default Widgets */
384 function wp_widget_pages( $args ) {
386 $options = get_option( 'widget_pages' );
388 $title = empty( $options['title'] ) ? __( 'Pages' ) : apply_filters('widget_title', $options['title']);
389 $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
390 $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
392 if ( $sortby == 'menu_order' ) {
393 $sortby = 'menu_order, post_title';
396 $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
398 if ( !empty( $out ) ) {
400 <?php echo $before_widget; ?>
401 <?php echo $before_title . $title . $after_title; ?>
405 <?php echo $after_widget; ?>
410 function wp_widget_pages_control() {
411 $options = $newoptions = get_option('widget_pages');
412 if ( $_POST['pages-submit'] ) {
413 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
415 $sortby = stripslashes( $_POST['pages-sortby'] );
417 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
418 $newoptions['sortby'] = $sortby;
420 $newoptions['sortby'] = 'menu_order';
423 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
425 if ( $options != $newoptions ) {
426 $options = $newoptions;
427 update_option('widget_pages', $options);
429 $title = attribute_escape($options['title']);
430 $exclude = attribute_escape( $options['exclude'] );
432 <p><label for="pages-title"><?php _e('Title:'); ?> <input class="widefat" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p>
434 <label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
435 <select name="pages-sortby" id="pages-sortby" class="widefat">
436 <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
437 <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
438 <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
443 <label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
445 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
447 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
451 function wp_widget_links($args) {
452 extract($args, EXTR_SKIP);
454 $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
455 wp_list_bookmarks(apply_filters('widget_links_args', array(
456 'title_before' => $before_title, 'title_after' => $after_title,
457 'category_before' => $before_widget, 'category_after' => $after_widget,
458 'show_images' => true, 'class' => 'linkcat widget'
462 function wp_widget_search($args) {
464 $searchform_template = get_template_directory() . '/searchform.php';
468 // Use current theme search form if it exists
469 if ( file_exists($searchform_template) ) {
470 include_once($searchform_template);
472 <form id="searchform" method="get" action="<?php bloginfo('url'); ?>/"><div>
473 <label class="hidden" for="s"><?php _e('Search for:'); ?></label>
474 <input type="text" name="s" id="s" size="15" value="<?php the_search_query(); ?>" />
475 <input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" />
482 function wp_widget_archives($args) {
484 $options = get_option('widget_archives');
485 $c = $options['count'] ? '1' : '0';
486 $d = $options['dropdown'] ? '1' : '0';
487 $title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);
490 echo $before_title . $title . $after_title;
494 <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
499 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
507 function wp_widget_archives_control() {
508 $options = $newoptions = get_option('widget_archives');
509 if ( $_POST["archives-submit"] ) {
510 $newoptions['count'] = isset($_POST['archives-count']);
511 $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
512 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
514 if ( $options != $newoptions ) {
515 $options = $newoptions;
516 update_option('widget_archives', $options);
518 $count = $options['count'] ? 'checked="checked"' : '';
519 $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
520 $title = attribute_escape($options['title']);
522 <p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
524 <label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
526 <label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
528 <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
532 function wp_widget_meta($args) {
534 $options = get_option('widget_meta');
535 $title = empty($options['title']) ? __('Meta') : apply_filters('widget_title', $options['title']);
537 <?php echo $before_widget; ?>
538 <?php echo $before_title . $title . $after_title; ?>
540 <?php wp_register(); ?>
541 <li><?php wp_loginout(); ?></li>
542 <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo attribute_escape(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
543 <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo attribute_escape(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
544 <li><a href="http://wordpress.org/" title="<?php echo attribute_escape(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
547 <?php echo $after_widget; ?>
550 function wp_widget_meta_control() {
551 $options = $newoptions = get_option('widget_meta');
552 if ( $_POST["meta-submit"] ) {
553 $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
555 if ( $options != $newoptions ) {
556 $options = $newoptions;
557 update_option('widget_meta', $options);
559 $title = attribute_escape($options['title']);
561 <p><label for="meta-title"><?php _e('Title:'); ?> <input class="widefat" id="meta-title" name="meta-title" type="text" value="<?php echo $title; ?>" /></label></p>
562 <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
566 function wp_widget_calendar($args) {
568 $options = get_option('widget_calendar');
569 $title = apply_filters('widget_title', $options['title']);
572 echo $before_widget . $before_title . $title . $after_title;
573 echo '<div id="calendar_wrap">';
578 function wp_widget_calendar_control() {
579 $options = $newoptions = get_option('widget_calendar');
580 if ( $_POST["calendar-submit"] ) {
581 $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
583 if ( $options != $newoptions ) {
584 $options = $newoptions;
585 update_option('widget_calendar', $options);
587 $title = attribute_escape($options['title']);
589 <p><label for="calendar-title"><?php _e('Title:'); ?> <input class="widefat" id="calendar-title" name="calendar-title" type="text" value="<?php echo $title; ?>" /></label></p>
590 <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
594 // See large comment section at end of this file
595 function wp_widget_text($args, $widget_args = 1) {
596 extract( $args, EXTR_SKIP );
597 if ( is_numeric($widget_args) )
598 $widget_args = array( 'number' => $widget_args );
599 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
600 extract( $widget_args, EXTR_SKIP );
602 $options = get_option('widget_text');
603 if ( !isset($options[$number]) )
606 $title = apply_filters('widget_title', $options[$number]['title']);
607 $text = apply_filters( 'widget_text', $options[$number]['text'] );
609 <?php echo $before_widget; ?>
610 <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
611 <div class="textwidget"><?php echo $text; ?></div>
612 <?php echo $after_widget; ?>
616 function wp_widget_text_control($widget_args) {
617 global $wp_registered_widgets;
618 static $updated = false;
620 if ( is_numeric($widget_args) )
621 $widget_args = array( 'number' => $widget_args );
622 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
623 extract( $widget_args, EXTR_SKIP );
625 $options = get_option('widget_text');
626 if ( !is_array($options) )
629 if ( !$updated && !empty($_POST['sidebar']) ) {
630 $sidebar = (string) $_POST['sidebar'];
632 $sidebars_widgets = wp_get_sidebars_widgets();
633 if ( isset($sidebars_widgets[$sidebar]) )
634 $this_sidebar =& $sidebars_widgets[$sidebar];
636 $this_sidebar = array();
638 foreach ( $this_sidebar as $_widget_id ) {
639 if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
640 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
641 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
642 unset($options[$widget_number]);
646 foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
647 if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
649 $title = strip_tags(stripslashes($widget_text['title']));
650 if ( current_user_can('unfiltered_html') )
651 $text = stripslashes( $widget_text['text'] );
653 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
654 $options[$widget_number] = compact( 'title', 'text' );
657 update_option('widget_text', $options);
661 if ( -1 == $number ) {
666 $title = attribute_escape($options[$number]['title']);
667 $text = format_to_edit($options[$number]['text']);
671 <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
672 <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
673 <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
678 function wp_widget_text_register() {
679 if ( !$options = get_option('widget_text') )
681 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
682 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
686 foreach ( array_keys($options) as $o ) {
687 // Old widgets can have null values for some reason
688 if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
690 $id = "text-$o"; // Never never never translate an id
691 wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
692 wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
695 // If there are none, we register the widget's existance with a generic template
697 wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
698 wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
702 // See large comment section at end of this file
703 function wp_widget_categories($args, $widget_args = 1) {
704 extract($args, EXTR_SKIP);
705 if ( is_numeric($widget_args) )
706 $widget_args = array( 'number' => $widget_args );
707 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
708 extract($widget_args, EXTR_SKIP);
710 $options = get_option('widget_categories');
711 if ( !isset($options[$number]) )
714 $c = $options[$number]['count'] ? '1' : '0';
715 $h = $options[$number]['hierarchical'] ? '1' : '0';
716 $d = $options[$number]['dropdown'] ? '1' : '0';
718 $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);
721 echo $before_title . $title . $after_title;
723 $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
726 $cat_args['show_option_none'] = __('Select Category');
727 wp_dropdown_categories($cat_args);
730 <script type='text/javascript'>
732 var dropdown = document.getElementById("cat");
733 function onCatChange() {
734 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
735 location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
738 dropdown.onchange = onCatChange;
747 $cat_args['title_li'] = '';
748 wp_list_categories($cat_args);
757 function wp_widget_categories_control( $widget_args ) {
758 global $wp_registered_widgets;
759 static $updated = false;
761 if ( is_numeric($widget_args) )
762 $widget_args = array( 'number' => $widget_args );
763 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
764 extract($widget_args, EXTR_SKIP);
766 $options = get_option('widget_categories');
768 if ( !is_array( $options ) )
771 if ( !$updated && !empty($_POST['sidebar']) ) {
772 $sidebar = (string) $_POST['sidebar'];
774 $sidebars_widgets = wp_get_sidebars_widgets();
775 if ( isset($sidebars_widgets[$sidebar]) )
776 $this_sidebar =& $sidebars_widgets[$sidebar];
778 $this_sidebar = array();
780 foreach ( $this_sidebar as $_widget_id ) {
781 if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
782 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
783 if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
784 unset($options[$widget_number]);
788 foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
789 if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
791 $title = trim(strip_tags(stripslashes($widget_cat['title'])));
792 $count = isset($widget_cat['count']);
793 $hierarchical = isset($widget_cat['hierarchical']);
794 $dropdown = isset($widget_cat['dropdown']);
795 $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
798 update_option('widget_categories', $options);
802 if ( -1 == $number ) {
805 $hierarchical = false;
809 $title = attribute_escape( $options[$number]['title'] );
810 $count = (bool) $options[$number]['count'];
811 $hierarchical = (bool) $options[$number]['hierarchical'];
812 $dropdown = (bool) $options[$number]['dropdown'];
816 <label for="categories-title-<?php echo $number; ?>">
817 <?php _e( 'Title:' ); ?>
818 <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
823 <label for="categories-dropdown-<?php echo $number; ?>">
824 <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown, true ); ?> />
825 <?php _e( 'Show as dropdown' ); ?>
828 <label for="categories-count-<?php echo $number; ?>">
829 <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count, true ); ?> />
830 <?php _e( 'Show post counts' ); ?>
833 <label for="categories-hierarchical-<?php echo $number; ?>">
834 <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical, true ); ?> />
835 <?php _e( 'Show hierarchy' ); ?>
839 <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
843 function wp_widget_categories_register() {
844 if ( !$options = get_option( 'widget_categories' ) )
847 if ( isset($options['title']) )
848 $options = wp_widget_categories_upgrade();
850 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
852 $name = __( 'Categories' );
855 foreach ( array_keys($options) as $o ) {
856 // Old widgets can have null values for some reason
857 if ( !isset($options[$o]['title']) )
859 $id = "categories-$o";
860 wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
861 wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
864 // If there are none, we register the widget's existance with a generic template
866 wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
867 wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
871 function wp_widget_categories_upgrade() {
872 $options = get_option( 'widget_categories' );
874 if ( !isset( $options['title'] ) )
877 $newoptions = array( 1 => $options );
879 update_option( 'widget_categories', $newoptions );
881 $sidebars_widgets = get_option( 'sidebars_widgets' );
882 if ( is_array( $sidebars_widgets ) ) {
883 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
884 if ( is_array( $widgets ) ) {
885 foreach ( $widgets as $widget )
886 $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
888 $new_widgets[$sidebar] = $widgets;
891 if ( $new_widgets != $sidebars_widgets )
892 update_option( 'sidebars_widgets', $new_widgets );
898 function wp_widget_recent_entries($args) {
899 if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
900 if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
901 return print($output);
906 $options = get_option('widget_recent_entries');
907 $title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']);
908 if ( !$number = (int) $options['number'] )
910 else if ( $number < 1 )
912 else if ( $number > 15 )
915 $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish'));
916 if ($r->have_posts()) :
918 <?php echo $before_widget; ?>
919 <?php echo $before_title . $title . $after_title; ?>
921 <?php while ($r->have_posts()) : $r->the_post(); ?>
922 <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
925 <?php echo $after_widget; ?>
927 wp_reset_query(); // Restore global post data stomped by the_post().
930 if ( '%BEG_OF_TITLE%' != $args['before_title'] )
931 wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
934 function wp_flush_widget_recent_entries() {
935 wp_cache_delete('widget_recent_entries', 'widget');
938 add_action('save_post', 'wp_flush_widget_recent_entries');
939 add_action('deleted_post', 'wp_flush_widget_recent_entries');
940 add_action('switch_theme', 'wp_flush_widget_recent_entries');
942 function wp_widget_recent_entries_control() {
943 $options = $newoptions = get_option('widget_recent_entries');
944 if ( $_POST["recent-entries-submit"] ) {
945 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
946 $newoptions['number'] = (int) $_POST["recent-entries-number"];
948 if ( $options != $newoptions ) {
949 $options = $newoptions;
950 update_option('widget_recent_entries', $options);
951 wp_flush_widget_recent_entries();
953 $title = attribute_escape($options['title']);
954 if ( !$number = (int) $options['number'] )
958 <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
960 <label for="recent-entries-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label>
962 <small><?php _e('(at most 15)'); ?></small>
964 <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
968 function wp_widget_recent_comments($args) {
969 global $wpdb, $comments, $comment;
970 extract($args, EXTR_SKIP);
971 $options = get_option('widget_recent_comments');
972 $title = empty($options['title']) ? __('Recent Comments') : apply_filters('widget_title', $options['title']);
973 if ( !$number = (int) $options['number'] )
975 else if ( $number < 1 )
977 else if ( $number > 15 )
980 if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
981 $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
982 wp_cache_add( 'recent_comments', $comments, 'widget' );
986 <?php echo $before_widget; ?>
987 <?php echo $before_title . $title . $after_title; ?>
988 <ul id="recentcomments"><?php
989 if ( $comments ) : foreach ($comments as $comment) :
990 echo '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
991 endforeach; endif;?></ul>
992 <?php echo $after_widget; ?>
996 function wp_delete_recent_comments_cache() {
997 wp_cache_delete( 'recent_comments', 'widget' );
999 add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
1000 add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
1002 function wp_widget_recent_comments_control() {
1003 $options = $newoptions = get_option('widget_recent_comments');
1004 if ( $_POST["recent-comments-submit"] ) {
1005 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
1006 $newoptions['number'] = (int) $_POST["recent-comments-number"];
1008 if ( $options != $newoptions ) {
1009 $options = $newoptions;
1010 update_option('widget_recent_comments', $options);
1011 wp_delete_recent_comments_cache();
1013 $title = attribute_escape($options['title']);
1014 if ( !$number = (int) $options['number'] )
1017 <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
1019 <label for="recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label>
1021 <small><?php _e('(at most 15)'); ?></small>
1023 <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
1027 function wp_widget_recent_comments_style() {
1029 <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
1033 function wp_widget_recent_comments_register() {
1034 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
1035 wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
1036 wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
1038 if ( is_active_widget('wp_widget_recent_comments') )
1039 add_action('wp_head', 'wp_widget_recent_comments_style');
1042 // See large comment section at end of this file
1043 function wp_widget_rss($args, $widget_args = 1) {
1044 extract($args, EXTR_SKIP);
1045 if ( is_numeric($widget_args) )
1046 $widget_args = array( 'number' => $widget_args );
1047 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1048 extract($widget_args, EXTR_SKIP);
1050 $options = get_option('widget_rss');
1052 if ( !isset($options[$number]) )
1055 if ( isset($options[$number]['error']) && $options[$number]['error'] )
1058 $url = $options[$number]['url'];
1059 while ( strstr($url, 'http') != $url )
1060 $url = substr($url, 1);
1064 require_once(ABSPATH . WPINC . '/rss.php');
1066 $rss = fetch_rss($url);
1067 $link = clean_url(strip_tags($rss->channel['link']));
1068 while ( strstr($link, 'http') != $link )
1069 $link = substr($link, 1);
1070 $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
1071 $title = $options[$number]['title'];
1072 if ( empty($title) )
1073 $title = htmlentities(strip_tags($rss->channel['title']));
1074 if ( empty($title) )
1076 if ( empty($title) )
1077 $title = __('Unknown Feed');
1078 $title = apply_filters('widget_title', $title );
1079 $url = clean_url(strip_tags($url));
1080 if ( file_exists(dirname(__FILE__) . '/rss.png') )
1081 $icon = str_replace(ABSPATH, site_url() . '/', dirname(__FILE__)) . '/rss.png';
1083 $icon = includes_url('images/rss.png');
1084 $title = "<a class='rsswidget' href='$url' title='" . attribute_escape(__('Syndicate this content')) ."'><img style='background:orange;color:white;border:none;' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
1086 echo $before_widget;
1087 echo $before_title . $title . $after_title;
1089 wp_widget_rss_output( $rss, $options[$number] );
1094 function wp_widget_rss_output( $rss, $args = array() ) {
1095 if ( is_string( $rss ) ) {
1096 require_once(ABSPATH . WPINC . '/rss.php');
1097 if ( !$rss = fetch_rss($rss) )
1099 } elseif ( is_array($rss) && isset($rss['url']) ) {
1100 require_once(ABSPATH . WPINC . '/rss.php');
1102 if ( !$rss = fetch_rss($rss['url']) )
1104 } elseif ( !is_object($rss) ) {
1108 extract( $args, EXTR_SKIP );
1110 $items = (int) $items;
1111 if ( $items < 1 || 20 < $items )
1113 $show_summary = (int) $show_summary;
1114 $show_author = (int) $show_author;
1115 $show_date = (int) $show_date;
1117 if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
1118 $rss->items = array_slice($rss->items, 0, $items);
1120 foreach ($rss->items as $item ) {
1121 while ( strstr($item['link'], 'http') != $item['link'] )
1122 $item['link'] = substr($item['link'], 1);
1123 $link = clean_url(strip_tags($item['link']));
1124 $title = attribute_escape(strip_tags($item['title']));
1125 if ( empty($title) )
1126 $title = __('Untitled');
1129 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
1130 $desc = $summary = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
1131 elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
1132 $desc = $summary = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
1134 if ( $show_summary ) {
1136 $summary = wp_specialchars( $summary );
1137 $summary = "<div class='rssSummary'>$summary</div>";
1144 if ( isset($item['pubdate']) )
1145 $date = $item['pubdate'];
1146 elseif ( isset($item['published']) )
1147 $date = $item['published'];
1150 if ( $date_stamp = strtotime( $date ) )
1151 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
1158 if ( $show_author ) {
1159 if ( isset($item['dc']['creator']) )
1160 $author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
1161 elseif ( isset($item['author_name']) )
1162 $author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
1165 if ( $link == '' ) {
1166 echo "<li>$title{$date}{$summary}{$author}</li>";
1168 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
1173 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
1177 function wp_widget_rss_control($widget_args) {
1178 global $wp_registered_widgets;
1179 static $updated = false;
1181 if ( is_numeric($widget_args) )
1182 $widget_args = array( 'number' => $widget_args );
1183 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1184 extract($widget_args, EXTR_SKIP);
1186 $options = get_option('widget_rss');
1187 if ( !is_array($options) )
1191 foreach ( $options as $option )
1192 if ( isset($option['url']) )
1193 $urls[$option['url']] = true;
1195 if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
1196 $sidebar = (string) $_POST['sidebar'];
1198 $sidebars_widgets = wp_get_sidebars_widgets();
1199 if ( isset($sidebars_widgets[$sidebar]) )
1200 $this_sidebar =& $sidebars_widgets[$sidebar];
1202 $this_sidebar = array();
1204 foreach ( $this_sidebar as $_widget_id ) {
1205 if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1206 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1207 if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
1208 unset($options[$widget_number]);
1212 foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
1213 if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
1215 $widget_rss = stripslashes_deep( $widget_rss );
1216 $url = sanitize_url(strip_tags($widget_rss['url']));
1217 $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
1220 update_option('widget_rss', $options);
1224 if ( -1 == $number ) {
1234 extract( (array) $options[$number] );
1237 wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
1240 function wp_widget_rss_form( $args, $inputs = null ) {
1241 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
1242 $inputs = wp_parse_args( $inputs, $default_inputs );
1244 $number = attribute_escape( $number );
1245 $title = attribute_escape( $title );
1246 $url = attribute_escape( $url );
1247 $items = (int) $items;
1248 if ( $items < 1 || 20 < $items )
1250 $show_summary = (int) $show_summary;
1251 $show_author = (int) $show_author;
1252 $show_date = (int) $show_date;
1254 if ( $inputs['url'] ) :
1257 <label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?>
1258 <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" />
1261 <?php endif; if ( $inputs['title'] ) : ?>
1263 <label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?>
1264 <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
1267 <?php endif; if ( $inputs['items'] ) : ?>
1269 <label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?>
1270 <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
1272 for ( $i = 1; $i <= 20; ++$i )
1273 echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
1278 <?php endif; if ( $inputs['show_summary'] ) : ?>
1280 <label for="rss-show-summary-<?php echo $number; ?>">
1281 <input id="rss-show-summary-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_summary]" type="checkbox" value="1" <?php if ( $show_summary ) echo 'checked="checked"'; ?>/>
1282 <?php _e('Display item content?'); ?>
1285 <?php endif; if ( $inputs['show_author'] ) : ?>
1287 <label for="rss-show-author-<?php echo $number; ?>">
1288 <input id="rss-show-author-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_author]" type="checkbox" value="1" <?php if ( $show_author ) echo 'checked="checked"'; ?>/>
1289 <?php _e('Display item author if available?'); ?>
1292 <?php endif; if ( $inputs['show_date'] ) : ?>
1294 <label for="rss-show-date-<?php echo $number; ?>">
1295 <input id="rss-show-date-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_date]" type="checkbox" value="1" <?php if ( $show_date ) echo 'checked="checked"'; ?>/>
1296 <?php _e('Display item date?'); ?>
1299 <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
1302 foreach ( array_keys($default_inputs) as $input ) :
1303 if ( 'hidden' === $inputs[$input] ) :
1304 $id = str_replace( '_', '-', $input );
1306 <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
1312 // Expects unescaped data
1313 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
1314 $items = (int) $widget_rss['items'];
1315 if ( $items < 1 || 20 < $items )
1317 $url = sanitize_url(strip_tags( $widget_rss['url'] ));
1318 $title = trim(strip_tags( $widget_rss['title'] ));
1319 $show_summary = (int) $widget_rss['show_summary'];
1320 $show_author = (int) $widget_rss['show_author'];
1321 $show_date = (int) $widget_rss['show_date'];
1323 if ( $check_feed ) {
1324 require_once(ABSPATH . WPINC . '/rss.php');
1325 $rss = fetch_rss($url);
1328 if ( !is_object($rss) ) {
1329 $url = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
1330 $error = sprintf(__('Error in RSS %1$d'), $widget_number );
1332 $link = clean_url(strip_tags($rss->channel['link']));
1333 while ( strstr($link, 'http') != $link )
1334 $link = substr($link, 1);
1338 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
1341 function wp_widget_rss_register() {
1342 if ( !$options = get_option('widget_rss') )
1344 $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
1345 $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
1349 foreach ( array_keys($options) as $o ) {
1350 // Old widgets can have null values for some reason
1351 if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
1353 $id = "rss-$o"; // Never never never translate an id
1354 wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
1355 wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
1358 // If there are none, we register the widget's existance with a generic template
1360 wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
1361 wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
1365 function wp_widget_tag_cloud($args) {
1367 $options = get_option('widget_tag_cloud');
1368 $title = empty($options['title']) ? __('Tags') : apply_filters('widget_title', $options['title']);
1370 echo $before_widget;
1371 echo $before_title . $title . $after_title;
1376 function wp_widget_tag_cloud_control() {
1377 $options = $newoptions = get_option('widget_tag_cloud');
1379 if ( $_POST['tag-cloud-submit'] ) {
1380 $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
1383 if ( $options != $newoptions ) {
1384 $options = $newoptions;
1385 update_option('widget_tag_cloud', $options);
1388 $title = attribute_escape( $options['title'] );
1390 <p><label for="tag-cloud-title">
1391 <?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
1393 <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
1397 function wp_widgets_init() {
1398 if ( !is_blog_installed() )
1401 $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
1402 wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
1403 wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
1405 $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
1406 wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
1407 wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
1409 $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
1410 wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
1411 wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
1413 $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
1414 wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
1416 $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
1417 wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
1418 wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
1420 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
1421 wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
1423 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
1424 wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
1425 wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
1427 $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
1428 wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
1429 wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
1431 wp_widget_categories_register();
1432 wp_widget_text_register();
1433 wp_widget_rss_register();
1434 wp_widget_recent_comments_register();
1436 do_action('widgets_init');
1439 add_action('init', 'wp_widgets_init', 1);
1441 /* Pattern for multi-widget (allows multiple instances such as the text widget).
1443 // Displays widget on blag
1444 // $widget_args: number
1445 // number: which of the several widgets of this type do we mean
1446 function widget_many( $args, $widget_args = 1 ) {
1447 extract( $args, EXTR_SKIP );
1448 if ( is_numeric($widget_args) )
1449 $widget_args = array( 'number' => $widget_args );
1450 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1451 extract( $widget_args, EXTR_SKIP );
1453 // Data should be stored as array: array( number => data for that instance of the widget, ... )
1454 $options = get_option('widget_many');
1455 if ( !isset($options[$number]) )
1458 echo $before_widget;
1460 // Do stuff for this widget, drawing data from $options[$number]
1465 // Displays form for a particular instance of the widget. Also updates the data after a POST submit
1466 // $widget_args: number
1467 // number: which of the several widgets of this type do we mean
1468 function widget_many_control( $widget_args = 1 ) {
1469 global $wp_registered_widgets;
1470 static $updated = false; // Whether or not we have already updated the data after a POST submit
1472 if ( is_numeric($widget_args) )
1473 $widget_args = array( 'number' => $widget_args );
1474 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1475 extract( $widget_args, EXTR_SKIP );
1477 // Data should be stored as array: array( number => data for that instance of the widget, ... )
1478 $options = get_option('widget_many');
1479 if ( !is_array($options) )
1482 // We need to update the data
1483 if ( !$updated && !empty($_POST['sidebar']) ) {
1484 // Tells us what sidebar to put the data in
1485 $sidebar = (string) $_POST['sidebar'];
1487 $sidebars_widgets = wp_get_sidebars_widgets();
1488 if ( isset($sidebars_widgets[$sidebar]) )
1489 $this_sidebar =& $sidebars_widgets[$sidebar];
1491 $this_sidebar = array();
1493 foreach ( $this_sidebar as $_widget_id ) {
1494 // Remove all widgets of this type from the sidebar. We'll add the new data in a second. This makes sure we don't get any duplicate data
1495 // since widget ids aren't necessarily persistent across multiple updates
1496 if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1497 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1498 if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
1499 unset($options[$widget_number]);
1503 foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
1504 // compile data from $widget_many_instance
1505 if ( !isset($widget_many_instance['something']) && isset($options[$widget_number]) ) // user clicked cancel
1507 $something = wp_specialchars( $widget_many_instance['something'] );
1508 $options[$widget_number] = array( 'something' => $something ); // Even simple widgets should store stuff in array, rather than in scalar
1511 update_option('widget_many', $options);
1513 $updated = true; // So that we don't go through this more than once
1517 // Here we echo out the form
1518 if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
1522 $something = attribute_escape($options[$number]['something']);
1525 // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
1526 // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
1529 <input class="widefat" id="widget-many-something-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][something]" type="text" value="<?php echo $data; ?>" />
1530 <input type="hidden" id="widget-many-submit-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][submit]" value="1" />
1535 // Registers each instance of our widget on startup
1536 function widget_many_register() {
1537 if ( !$options = get_option('widget_many') )
1540 $widget_ops = array('classname' => 'widget_many', 'description' => __('Widget which allows multiple instances'));
1541 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
1544 $registered = false;
1545 foreach ( array_keys($options) as $o ) {
1546 // Old widgets can have null values for some reason
1547 if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
1550 // $id should look like {$id_base}-{$o}
1551 $id = "many-$o"; // Never never never translate an id
1553 wp_register_sidebar_widget( $id, $name, 'widget_many', $widget_ops, array( 'number' => $o ) );
1554 wp_register_widget_control( $id, $name, 'widget_many_control', $control_ops, array( 'number' => $o ) );
1557 // If there are none, we register the widget's existance with a generic template
1558 if ( !$registered ) {
1559 wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
1560 wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
1564 // This is important
1565 add_action( 'widgets_init', 'widget_many_register' )