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, $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 if ( empty($output_callback) ) {
99 unset($wp_registered_widgets[$id]);
103 $defaults = array('classname' => $output_callback);
104 $options = wp_parse_args($options, $defaults);
108 'callback' => $output_callback,
109 'params' => array_slice(func_get_args(), 4)
111 $widget = array_merge($widget, $options);
113 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
114 $wp_registered_widgets[$id] = $widget;
117 function wp_widget_description( $id ) {
118 if ( !is_scalar($id) )
121 global $wp_registered_widgets;
123 if ( isset($wp_registered_widgets[$id]['description']) )
124 return wp_specialchars( $wp_registered_widgets[$id]['description'] );
127 function unregister_sidebar_widget($id) {
128 return wp_unregister_sidebar_widget($id);
131 function wp_unregister_sidebar_widget($id) {
132 wp_register_sidebar_widget($id, '', '');
133 wp_unregister_widget_control($id);
136 function register_widget_control($name, $control_callback, $width = '', $height = '') {
138 if ( is_array($name) ) {
139 if ( count($name) == 3 )
140 $name = sprintf($name[0], $name[2]);
145 $id = sanitize_title($name);
147 if ( !empty($width) )
148 $options['width'] = $width;
149 if ( !empty($height) )
150 $options['height'] = $height;
151 $params = array_slice(func_get_args(), 4);
152 $args = array($id, $name, $control_callback, $options);
153 if ( !empty($params) )
154 $args = array_merge($args, $params);
156 call_user_func_array('wp_register_widget_control', $args);
159 /* $options: height, width, id_base
161 * width: width of fully expanded control form. Try hard to use the default width.
162 * id_base: for multi-widgets (widgets which allow multiple instances such as the text widget), an id_base must be provided.
163 * the widget id will ennd up looking like {$id_base}-{$unique_number}
165 function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
166 global $wp_registered_widget_controls;
168 if ( empty($control_callback) ) {
169 unset($wp_registered_widget_controls[$id]);
173 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
176 $defaults = array('width' => 250, 'height' => 200 ); // height is never used
177 $options = wp_parse_args($options, $defaults);
178 $options['width'] = (int) $options['width'];
179 $options['height'] = (int) $options['height'];
184 'callback' => $control_callback,
185 'params' => array_slice(func_get_args(), 4)
187 $widget = array_merge($widget, $options);
189 $wp_registered_widget_controls[$id] = $widget;
192 function unregister_widget_control($id) {
193 return wp_unregister_widget_control($id);
196 function wp_unregister_widget_control($id) {
197 return wp_register_widget_control($id, '', '');
200 function dynamic_sidebar($index = 1) {
201 global $wp_registered_sidebars, $wp_registered_widgets;
203 if ( is_int($index) ) {
204 $index = "sidebar-$index";
206 $index = sanitize_title($index);
207 foreach ( $wp_registered_sidebars as $key => $value ) {
208 if ( sanitize_title($value['name']) == $index ) {
215 $sidebars_widgets = wp_get_sidebars_widgets();
217 if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
220 $sidebar = $wp_registered_sidebars[$index];
223 foreach ( $sidebars_widgets[$index] as $id ) {
224 $params = array_merge(
225 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
226 (array) $wp_registered_widgets[$id]['params']
229 // Substitute HTML id and class attributes into before_widget
231 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
232 if ( is_string($cn) )
233 $classname_ .= '_' . $cn;
234 elseif ( is_object($cn) )
235 $classname_ .= '_' . get_class($cn);
237 $classname_ = ltrim($classname_, '_');
238 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
240 $params = apply_filters( 'dynamic_sidebar_params', $params );
242 $callback = $wp_registered_widgets[$id]['callback'];
244 if ( is_callable($callback) ) {
245 call_user_func_array($callback, $params);
253 /* @return mixed false if widget is not active or id of sidebar in which the widget is active
255 function is_active_widget($callback, $widget_id = false) {
256 global $wp_registered_widgets;
258 $sidebars_widgets = wp_get_sidebars_widgets(false);
260 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
261 if ( is_array($widgets) ) foreach ( $widgets as $widget )
262 if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
263 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
270 function is_dynamic_sidebar() {
271 global $wp_registered_widgets, $wp_registered_sidebars;
272 $sidebars_widgets = get_option('sidebars_widgets');
273 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
274 if ( count($sidebars_widgets[$index]) ) {
275 foreach ( $sidebars_widgets[$index] as $widget )
276 if ( array_key_exists($widget, $wp_registered_widgets) )
283 /* Internal Functions */
285 function wp_get_sidebars_widgets($update = true) {
286 global $wp_registered_widgets, $wp_registered_sidebars;
288 $sidebars_widgets = get_option('sidebars_widgets');
289 $_sidebars_widgets = array();
291 if ( !isset($sidebars_widgets['array_version']) )
292 $sidebars_widgets['array_version'] = 1;
294 switch ( $sidebars_widgets['array_version'] ) {
296 foreach ( $sidebars_widgets as $index => $sidebar )
297 if ( is_array($sidebar) )
298 foreach ( $sidebar as $i => $name ) {
299 $id = strtolower($name);
300 if ( isset($wp_registered_widgets[$id]) ) {
301 $_sidebars_widgets[$index][$i] = $id;
304 $id = sanitize_title($name);
305 if ( isset($wp_registered_widgets[$id]) ) {
306 $_sidebars_widgets[$index][$i] = $id;
312 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
313 if ( strtolower($widget['name']) == strtolower($name) ) {
314 $_sidebars_widgets[$index][$i] = $widget['id'];
317 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
318 $_sidebars_widgets[$index][$i] = $widget['id'];
327 unset($_sidebars_widgets[$index][$i]);
329 $_sidebars_widgets['array_version'] = 2;
330 $sidebars_widgets = $_sidebars_widgets;
331 unset($_sidebars_widgets);
334 $sidebars = array_keys( $wp_registered_sidebars );
335 if ( !empty( $sidebars ) ) {
336 // Move the known-good ones first
337 foreach ( $sidebars as $id ) {
338 if ( array_key_exists( $id, $sidebars_widgets ) ) {
339 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
340 unset($sidebars_widgets[$id], $sidebars[$id]);
344 // Assign to each unmatched registered sidebar the first available orphan
345 unset( $sidebars_widgets[ 'array_version' ] );
346 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
347 $_sidebars_widgets[ $sidebar ] = $widgets;
349 $_sidebars_widgets['array_version'] = 3;
350 $sidebars_widgets = $_sidebars_widgets;
351 unset($_sidebars_widgets);
355 update_option('sidebars_widgets', $sidebars_widgets);
358 unset($sidebars_widgets['array_version']);
360 return $sidebars_widgets;
363 function wp_set_sidebars_widgets( $sidebars_widgets ) {
364 update_option( 'sidebars_widgets', $sidebars_widgets );
367 function wp_get_widget_defaults() {
368 global $wp_registered_sidebars;
372 foreach ( $wp_registered_sidebars as $index => $sidebar )
373 $defaults[$index] = array();
378 /* Default Widgets */
380 function wp_widget_pages( $args ) {
382 $options = get_option( 'widget_pages' );
384 $title = empty( $options['title'] ) ? __( 'Pages' ) : $options['title'];
385 $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
386 $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
388 if ( $sortby == 'menu_order' ) {
389 $sortby = 'menu_order, post_title';
392 $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
394 if ( !empty( $out ) ) {
396 <?php echo $before_widget; ?>
397 <?php echo $before_title . $title . $after_title; ?>
401 <?php echo $after_widget; ?>
406 function wp_widget_pages_control() {
407 $options = $newoptions = get_option('widget_pages');
408 if ( $_POST['pages-submit'] ) {
409 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
411 $sortby = stripslashes( $_POST['pages-sortby'] );
413 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
414 $newoptions['sortby'] = $sortby;
416 $newoptions['sortby'] = 'menu_order';
419 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
421 if ( $options != $newoptions ) {
422 $options = $newoptions;
423 update_option('widget_pages', $options);
425 $title = attribute_escape($options['title']);
426 $exclude = attribute_escape( $options['exclude'] );
428 <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>
430 <label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
431 <select name="pages-sortby" id="pages-sortby" class="widefat">
432 <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
433 <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
434 <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
439 <label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
441 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
443 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
447 function wp_widget_links($args) {
448 extract($args, EXTR_SKIP);
450 $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
451 wp_list_bookmarks(array(
452 'title_before' => $before_title, 'title_after' => $after_title,
453 'category_before' => $before_widget, 'category_after' => $after_widget,
454 'show_images' => true, 'class' => 'linkcat widget'
458 function wp_widget_search($args) {
461 <?php echo $before_widget; ?>
462 <form id="searchform" method="get" action="<?php bloginfo('home'); ?>">
464 <input type="text" name="s" id="s" size="15" /><br />
465 <input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" />
468 <?php echo $after_widget; ?>
472 function wp_widget_archives($args) {
474 $options = get_option('widget_archives');
475 $c = $options['count'] ? '1' : '0';
476 $d = $options['dropdown'] ? '1' : '0';
477 $title = empty($options['title']) ? __('Archives') : $options['title'];
480 echo $before_title . $title . $after_title;
484 <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>
489 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
497 function wp_widget_archives_control() {
498 $options = $newoptions = get_option('widget_archives');
499 if ( $_POST["archives-submit"] ) {
500 $newoptions['count'] = isset($_POST['archives-count']);
501 $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
502 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
504 if ( $options != $newoptions ) {
505 $options = $newoptions;
506 update_option('widget_archives', $options);
508 $count = $options['count'] ? 'checked="checked"' : '';
509 $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
510 $title = attribute_escape($options['title']);
512 <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>
514 <label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
516 <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>
518 <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
522 function wp_widget_meta($args) {
524 $options = get_option('widget_meta');
525 $title = empty($options['title']) ? __('Meta') : $options['title'];
527 <?php echo $before_widget; ?>
528 <?php echo $before_title . $title . $after_title; ?>
530 <?php wp_register(); ?>
531 <li><?php wp_loginout(); ?></li>
532 <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>
533 <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>
534 <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>
537 <?php echo $after_widget; ?>
540 function wp_widget_meta_control() {
541 $options = $newoptions = get_option('widget_meta');
542 if ( $_POST["meta-submit"] ) {
543 $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
545 if ( $options != $newoptions ) {
546 $options = $newoptions;
547 update_option('widget_meta', $options);
549 $title = attribute_escape($options['title']);
551 <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>
552 <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
556 function wp_widget_calendar($args) {
558 $options = get_option('widget_calendar');
559 $title = $options['title'];
562 echo $before_widget . $before_title . $title . $after_title;
563 echo '<div id="calendar_wrap">';
568 function wp_widget_calendar_control() {
569 $options = $newoptions = get_option('widget_calendar');
570 if ( $_POST["calendar-submit"] ) {
571 $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
573 if ( $options != $newoptions ) {
574 $options = $newoptions;
575 update_option('widget_calendar', $options);
577 $title = attribute_escape($options['title']);
579 <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>
580 <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
584 // See large comment section at end of this file
585 function wp_widget_text($args, $widget_args = 1) {
586 extract( $args, EXTR_SKIP );
587 if ( is_numeric($widget_args) )
588 $widget_args = array( 'number' => $widget_args );
589 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
590 extract( $widget_args, EXTR_SKIP );
592 $options = get_option('widget_text');
593 if ( !isset($options[$number]) )
596 $title = $options[$number]['title'];
597 $text = apply_filters( 'widget_text', $options[$number]['text'] );
599 <?php echo $before_widget; ?>
600 <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
601 <div class="textwidget"><?php echo $text; ?></div>
602 <?php echo $after_widget; ?>
606 function wp_widget_text_control($widget_args) {
607 global $wp_registered_widgets;
608 static $updated = false;
610 if ( is_numeric($widget_args) )
611 $widget_args = array( 'number' => $widget_args );
612 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
613 extract( $widget_args, EXTR_SKIP );
615 $options = get_option('widget_text');
616 if ( !is_array($options) )
619 if ( !$updated && !empty($_POST['sidebar']) ) {
620 $sidebar = (string) $_POST['sidebar'];
622 $sidebars_widgets = wp_get_sidebars_widgets();
623 if ( isset($sidebars_widgets[$sidebar]) )
624 $this_sidebar =& $sidebars_widgets[$sidebar];
626 $this_sidebar = array();
628 foreach ( $this_sidebar as $_widget_id ) {
629 if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
630 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
631 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
632 unset($options[$widget_number]);
636 foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
637 if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
639 $title = strip_tags(stripslashes($widget_text['title']));
640 if ( current_user_can('unfiltered_html') )
641 $text = stripslashes( $widget_text['text'] );
643 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
644 $options[$widget_number] = compact( 'title', 'text' );
647 update_option('widget_text', $options);
651 if ( -1 == $number ) {
656 $title = attribute_escape($options[$number]['title']);
657 $text = format_to_edit($options[$number]['text']);
661 <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
662 <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
663 <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
668 function wp_widget_text_register() {
669 if ( !$options = get_option('widget_text') )
671 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
672 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
676 foreach ( array_keys($options) as $o ) {
677 // Old widgets can have null values for some reason
678 if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
680 $id = "text-$o"; // Never never never translate an id
681 wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
682 wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
685 // If there are none, we register the widget's existance with a generic template
687 wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
688 wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
692 // See large comment section at end of this file
693 function wp_widget_categories($args, $widget_args = 1) {
694 extract($args, EXTR_SKIP);
695 if ( is_numeric($widget_args) )
696 $widget_args = array( 'number' => $widget_args );
697 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
698 extract($widget_args, EXTR_SKIP);
700 $options = get_option('widget_categories');
701 if ( !isset($options[$number]) )
704 $c = $options[$number]['count'] ? '1' : '0';
705 $h = $options[$number]['hierarchical'] ? '1' : '0';
706 $d = $options[$number]['dropdown'] ? '1' : '0';
708 $title = empty($options[$number]['title']) ? __('Categories') : $options[$number]['title'];
711 echo $before_title . $title . $after_title;
713 $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}";
716 wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category'));
719 <script type='text/javascript'>
721 var dropdown = document.getElementById("cat");
722 function onCatChange() {
723 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
724 location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
727 dropdown.onchange = onCatChange;
735 <?php wp_list_categories($cat_args . '&title_li='); ?>
743 function wp_widget_categories_control( $widget_args ) {
744 global $wp_registered_widgets;
745 static $updated = false;
747 if ( is_numeric($widget_args) )
748 $widget_args = array( 'number' => $widget_args );
749 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
750 extract($widget_args, EXTR_SKIP);
752 $options = get_option('widget_categories');
754 if ( !is_array( $options ) )
757 if ( !$updated && !empty($_POST['sidebar']) ) {
758 $sidebar = (string) $_POST['sidebar'];
760 $sidebars_widgets = wp_get_sidebars_widgets();
761 if ( isset($sidebars_widgets[$sidebar]) )
762 $this_sidebar =& $sidebars_widgets[$sidebar];
764 $this_sidebar = array();
766 foreach ( $this_sidebar as $_widget_id ) {
767 if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
768 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
769 if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
770 unset($options[$widget_number]);
774 foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
775 if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
777 $title = trim(strip_tags(stripslashes($widget_cat['title'])));
778 $count = isset($widget_cat['count']);
779 $hierarchical = isset($widget_cat['hierarchical']);
780 $dropdown = isset($widget_cat['dropdown']);
781 $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
784 update_option('widget_categories', $options);
788 if ( -1 == $number ) {
791 $hierarchical = false;
795 $title = attribute_escape( $options[$number]['title'] );
796 $count = (bool) $options[$number]['count'];
797 $hierarchical = (bool) $options[$number]['hierarchical'];
798 $dropdown = (bool) $options[$number]['dropdown'];
802 <label for="categories-title-<?php echo $number; ?>">
803 <?php _e( 'Title:' ); ?>
804 <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
809 <label for="categories-dropdown-<?php echo $number; ?>">
810 <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown, true ); ?> />
811 <?php _e( 'Show as dropdown' ); ?>
814 <label for="categories-count-<?php echo $number; ?>">
815 <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count, true ); ?> />
816 <?php _e( 'Show post counts' ); ?>
819 <label for="categories-hierarchical-<?php echo $number; ?>">
820 <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical, true ); ?> />
821 <?php _e( 'Show hierarchy' ); ?>
825 <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
829 function wp_widget_categories_register() {
830 if ( !$options = get_option( 'widget_categories' ) )
833 if ( isset($options['title']) )
834 $options = wp_widget_categories_upgrade();
836 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
838 $name = __( 'Categories' );
841 foreach ( array_keys($options) as $o ) {
842 // Old widgets can have null values for some reason
843 if ( !isset($options[$o]['title']) )
845 $id = "categories-$o";
846 wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
847 wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
850 // If there are none, we register the widget's existance with a generic template
852 wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
853 wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
857 function wp_widget_categories_upgrade() {
858 $options = get_option( 'widget_categories' );
860 if ( !isset( $options['title'] ) )
863 $newoptions = array( 1 => $options );
865 update_option( 'widget_categories', $newoptions );
867 $sidebars_widgets = get_option( 'sidebars_widgets' );
868 if ( is_array( $sidebars_widgets ) ) {
869 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
870 if ( is_array( $widgets ) ) {
871 foreach ( $widgets as $widget )
872 $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
874 $new_widgets[$sidebar] = $widgets;
877 if ( $new_widgets != $sidebars_widgets )
878 update_option( 'sidebars_widgets', $new_widgets );
884 function wp_widget_recent_entries($args) {
885 if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
886 if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
887 return print($output);
892 $options = get_option('widget_recent_entries');
893 $title = empty($options['title']) ? __('Recent Posts') : $options['title'];
894 if ( !$number = (int) $options['number'] )
896 else if ( $number < 1 )
898 else if ( $number > 15 )
901 $r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0&post_status=publish");
902 if ($r->have_posts()) :
904 <?php echo $before_widget; ?>
905 <?php echo $before_title . $title . $after_title; ?>
907 <?php while ($r->have_posts()) : $r->the_post(); ?>
908 <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
911 <?php echo $after_widget; ?>
913 wp_reset_query(); // Restore global post data stomped by the_post().
916 if ( '%BEG_OF_TITLE%' != $args['before_title'] )
917 wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
920 function wp_flush_widget_recent_entries() {
921 wp_cache_delete('widget_recent_entries', 'widget');
924 add_action('save_post', 'wp_flush_widget_recent_entries');
925 add_action('deleted_post', 'wp_flush_widget_recent_entries');
926 add_action('switch_theme', 'wp_flush_widget_recent_entries');
928 function wp_widget_recent_entries_control() {
929 $options = $newoptions = get_option('widget_recent_entries');
930 if ( $_POST["recent-entries-submit"] ) {
931 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
932 $newoptions['number'] = (int) $_POST["recent-entries-number"];
934 if ( $options != $newoptions ) {
935 $options = $newoptions;
936 update_option('widget_recent_entries', $options);
937 wp_flush_widget_recent_entries();
939 $title = attribute_escape($options['title']);
940 if ( !$number = (int) $options['number'] )
944 <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>
946 <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>
948 <small><?php _e('(at most 15)'); ?></small>
950 <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
954 function wp_widget_recent_comments($args) {
955 global $wpdb, $comments, $comment;
956 extract($args, EXTR_SKIP);
957 $options = get_option('widget_recent_comments');
958 $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
959 if ( !$number = (int) $options['number'] )
961 else if ( $number < 1 )
963 else if ( $number > 15 )
966 if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
967 $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");
968 wp_cache_add( 'recent_comments', $comments, 'widget' );
972 <?php echo $before_widget; ?>
973 <?php echo $before_title . $title . $after_title; ?>
974 <ul id="recentcomments"><?php
975 if ( $comments ) : foreach ($comments as $comment) :
976 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>';
977 endforeach; endif;?></ul>
978 <?php echo $after_widget; ?>
982 function wp_delete_recent_comments_cache() {
983 wp_cache_delete( 'recent_comments', 'widget' );
985 add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
986 add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
988 function wp_widget_recent_comments_control() {
989 $options = $newoptions = get_option('widget_recent_comments');
990 if ( $_POST["recent-comments-submit"] ) {
991 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
992 $newoptions['number'] = (int) $_POST["recent-comments-number"];
994 if ( $options != $newoptions ) {
995 $options = $newoptions;
996 update_option('widget_recent_comments', $options);
997 wp_delete_recent_comments_cache();
999 $title = attribute_escape($options['title']);
1000 if ( !$number = (int) $options['number'] )
1003 <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>
1005 <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>
1007 <small><?php _e('(at most 15)'); ?></small>
1009 <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
1013 function wp_widget_recent_comments_style() {
1015 <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
1019 function wp_widget_recent_comments_register() {
1020 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
1021 wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
1022 wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
1024 if ( is_active_widget('wp_widget_recent_comments') )
1025 add_action('wp_head', 'wp_widget_recent_comments_style');
1028 // See large comment section at end of this file
1029 function wp_widget_rss($args, $widget_args = 1) {
1030 extract($args, EXTR_SKIP);
1031 if ( is_numeric($widget_args) )
1032 $widget_args = array( 'number' => $widegt_args );
1033 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1034 extract($widget_args, EXTR_SKIP);
1036 $options = get_option('widget_rss');
1038 if ( !isset($options[$number]) )
1041 if ( isset($options[$number]['error']) && $options[$number]['error'] )
1044 $url = $options[$number]['url'];
1045 while ( strstr($url, 'http') != $url )
1046 $url = substr($url, 1);
1050 require_once(ABSPATH . WPINC . '/rss.php');
1052 $rss = fetch_rss($url);
1053 $link = clean_url(strip_tags($rss->channel['link']));
1054 while ( strstr($link, 'http') != $link )
1055 $link = substr($link, 1);
1056 $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
1057 $title = $options[$number]['title'];
1058 if ( empty($title) )
1059 $title = htmlentities(strip_tags($rss->channel['title']));
1060 if ( empty($title) )
1062 if ( empty($title) )
1063 $title = __('Unknown Feed');
1064 $url = clean_url(strip_tags($url));
1065 if ( file_exists(dirname(__FILE__) . '/rss.png') )
1066 $icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png';
1068 $icon = get_option('siteurl').'/wp-includes/images/rss.png';
1069 $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>";
1071 echo $before_widget;
1072 echo $before_title . $title . $after_title;
1074 wp_widget_rss_output( $rss, $options[$number] );
1079 function wp_widget_rss_output( $rss, $args = array() ) {
1080 if ( is_string( $rss ) ) {
1081 require_once(ABSPATH . WPINC . '/rss.php');
1082 if ( !$rss = fetch_rss($rss) )
1084 } elseif ( is_array($rss) && isset($rss['url']) ) {
1085 require_once(ABSPATH . WPINC . '/rss.php');
1087 if ( !$rss = fetch_rss($rss['url']) )
1089 } elseif ( !is_object($rss) ) {
1093 extract( $args, EXTR_SKIP );
1095 $items = (int) $items;
1096 if ( $items < 1 || 20 < $items )
1098 $show_summary = (int) $show_summary;
1099 $show_author = (int) $show_author;
1100 $show_date = (int) $show_date;
1102 if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
1103 $rss->items = array_slice($rss->items, 0, $items);
1105 foreach ($rss->items as $item ) {
1106 while ( strstr($item['link'], 'http') != $item['link'] )
1107 $item['link'] = substr($item['link'], 1);
1108 $link = clean_url(strip_tags($item['link']));
1109 $title = attribute_escape(strip_tags($item['title']));
1110 if ( empty($title) )
1111 $title = __('Untitled');
1113 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
1114 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
1115 elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
1116 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
1119 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
1120 $summary = $item['description'];
1121 elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
1122 $summary = $item['summary'];
1124 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($summary, ENT_QUOTES))));
1126 if ( $show_summary ) {
1128 $summary = wp_specialchars( $summary );
1129 $summary = "<div class='rssSummary'>$summary</div>";
1136 if ( isset($item['pubdate']) )
1137 $date = $item['pubdate'];
1138 elseif ( isset($item['published']) )
1139 $date = $item['published'];
1142 if ( $date_stamp = strtotime( $date ) )
1143 $date = '<span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
1150 if ( $show_author ) {
1151 if ( isset($item['dc']['creator']) )
1152 $author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
1153 elseif ( isset($item['author_name']) )
1154 $author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
1157 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
1161 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
1165 function wp_widget_rss_control($widget_args) {
1166 global $wp_registered_widgets;
1167 static $updated = false;
1169 if ( is_numeric($widget_args) )
1170 $widget_args = array( 'number' => $widget_args );
1171 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1172 extract($widget_args, EXTR_SKIP);
1174 $options = get_option('widget_rss');
1175 if ( !is_array($options) )
1179 foreach ( $options as $option )
1180 if ( isset($option['url']) )
1181 $urls[$option['url']] = true;
1183 if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
1184 $sidebar = (string) $_POST['sidebar'];
1186 $sidebars_widgets = wp_get_sidebars_widgets();
1187 if ( isset($sidebars_widgets[$sidebar]) )
1188 $this_sidebar =& $sidebars_widgets[$sidebar];
1190 $this_sidebar = array();
1192 foreach ( $this_sidebar as $_widget_id ) {
1193 if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1194 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1195 if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
1196 unset($options[$widget_number]);
1200 foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
1201 if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
1203 $widget_rss = stripslashes_deep( $widget_rss );
1204 $url = sanitize_url(strip_tags($widget_rss['url']));
1205 $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
1208 update_option('widget_rss', $options);
1212 if ( -1 == $number ) {
1222 extract( (array) $options[$number] );
1225 wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
1228 function wp_widget_rss_form( $args, $inputs = null ) {
1229 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
1230 $inputs = wp_parse_args( $inputs, $default_inputs );
1232 $number = attribute_escape( $number );
1233 $title = attribute_escape( $title );
1234 $url = attribute_escape( $url );
1235 $items = (int) $items;
1236 if ( $items < 1 || 20 < $items )
1238 $show_summary = (int) $show_summary;
1239 $show_author = (int) $show_author;
1240 $show_date = (int) $show_date;
1242 if ( $inputs['url'] ) :
1245 <label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?>
1246 <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" />
1249 <?php endif; if ( $inputs['title'] ) : ?>
1251 <label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?>
1252 <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
1255 <?php endif; if ( $inputs['items'] ) : ?>
1257 <label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?>
1258 <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
1260 for ( $i = 1; $i <= 20; ++$i )
1261 echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
1266 <?php endif; if ( $inputs['show_summary'] ) : ?>
1268 <label for="rss-show-summary-<?php echo $number; ?>">
1269 <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"'; ?>/>
1270 <?php _e('Display item content?'); ?>
1273 <?php endif; if ( $inputs['show_author'] ) : ?>
1275 <label for="rss-show-author-<?php echo $number; ?>">
1276 <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"'; ?>/>
1277 <?php _e('Display item author if available?'); ?>
1280 <?php endif; if ( $inputs['show_date'] ) : ?>
1282 <label for="rss-show-date-<?php echo $number; ?>">
1283 <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"'; ?>/>
1284 <?php _e('Display item date?'); ?>
1287 <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
1290 foreach ( array_keys($default_inputs) as $input ) :
1291 if ( 'hidden' === $inputs[$input] ) :
1292 $id = str_replace( '_', '-', $input );
1294 <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
1300 // Expects unescaped data
1301 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
1302 $items = (int) $widget_rss['items'];
1303 if ( $items < 1 || 20 < $items )
1305 $url = sanitize_url(strip_tags( $widget_rss['url'] ));
1306 $title = trim(strip_tags( $widget_rss['title'] ));
1307 $show_summary = (int) $widget_rss['show_summary'];
1308 $show_author = (int) $widget_rss['show_author'];
1309 $show_date = (int) $widget_rss['show_date'];
1311 if ( $check_feed ) {
1312 require_once(ABSPATH . WPINC . '/rss.php');
1313 $rss = fetch_rss($url);
1316 if ( !is_object($rss) ) {
1317 $url = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
1318 $error = sprintf(__('Error in RSS %1$d'), $widget_number );
1320 $link = clean_url(strip_tags($rss->channel['link']));
1321 while ( strstr($link, 'http') != $link )
1322 $link = substr($link, 1);
1326 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
1329 function wp_widget_rss_register() {
1330 if ( !$options = get_option('widget_rss') )
1332 $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
1333 $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
1337 foreach ( array_keys($options) as $o ) {
1338 // Old widgets can have null values for some reason
1339 if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
1341 $id = "rss-$o"; // Never never never translate an id
1342 wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
1343 wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
1346 // If there are none, we register the widget's existance with a generic template
1348 wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
1349 wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
1353 function wp_widget_tag_cloud($args) {
1355 $options = get_option('widget_tag_cloud');
1356 $title = empty($options['title']) ? __('Tags') : $options['title'];
1358 echo $before_widget;
1359 echo $before_title . $title . $after_title;
1364 function wp_widget_tag_cloud_control() {
1365 $options = $newoptions = get_option('widget_tag_cloud');
1367 if ( $_POST['tag-cloud-submit'] ) {
1368 $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
1371 if ( $options != $newoptions ) {
1372 $options = $newoptions;
1373 update_option('widget_tag_cloud', $options);
1376 $title = attribute_escape( $options['title'] );
1378 <p><label for="tag-cloud-title">
1379 <?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
1381 <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
1385 function wp_widgets_init() {
1386 if ( !is_blog_installed() )
1389 $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
1390 wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
1391 wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
1393 $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
1394 wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
1395 wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
1397 $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
1398 wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
1399 wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
1401 $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
1402 wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
1404 $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
1405 wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
1406 wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
1408 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
1409 wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
1411 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
1412 wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
1413 wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
1415 $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
1416 wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
1417 wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
1419 wp_widget_categories_register();
1420 wp_widget_text_register();
1421 wp_widget_rss_register();
1422 wp_widget_recent_comments_register();
1424 do_action('widgets_init');
1427 add_action('init', 'wp_widgets_init', 1);
1429 /* Pattern for multi-widget (allows multiple instances such as the text widget).
1431 // Displays widget on blag
1432 // $widget_args: number
1433 // number: which of the several widgets of this type do we mean
1434 function widget_many( $args, $widget_args = 1 ) {
1435 extract( $args, EXTR_SKIP );
1436 if ( is_numeric($widget_args) )
1437 $widget_args = array( 'number' => $widget_args );
1438 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1439 extract( $widget_args, EXTR_SKIP );
1441 // Data should be stored as array: array( number => data for that instance of the widget, ... )
1442 $options = get_option('widget_many');
1443 if ( !isset($options[$number]) )
1446 echo $before_widget;
1448 // Do stuff for this widget, drawing data from $options[$number]
1453 // Displays form for a particular instance of the widget. Also updates the data after a POST submit
1454 // $widget_args: number
1455 // number: which of the several widgets of this type do we mean
1456 function widget_many_control( $widget_args = 1 ) {
1457 global $wp_registered_widgets;
1458 static $updated = false; // Whether or not we have already updated the data after a POST submit
1460 if ( is_numeric($widget_args) )
1461 $widget_args = array( 'number' => $widget_args );
1462 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1463 extract( $widget_args, EXTR_SKIP );
1465 // Data should be stored as array: array( number => data for that instance of the widget, ... )
1466 $options = get_option('widget_many');
1467 if ( !is_array($options) )
1470 // We need to update the data
1471 if ( !$updated && !empty($_POST['sidebar']) ) {
1472 // Tells us what sidebar to put the data in
1473 $sidebar = (string) $_POST['sidebar'];
1475 $sidebars_widgets = wp_get_sidebars_widgets();
1476 if ( isset($sidebars_widgets[$sidebar]) )
1477 $this_sidebar =& $sidebars_widgets[$sidebar];
1479 $this_sidebar = array();
1481 foreach ( $this_sidebar as $_widget_id ) {
1482 // 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
1483 // since widget ids aren't necessarily persistent across multiple updates
1484 if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1485 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1486 if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
1487 unset($options[$widget_number]);
1491 foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
1492 // compile data from $widget_many_instance
1493 if ( !isset($widget_many_instance['something']) && isset($options[$widget_number]) ) // user clicked cancel
1495 $something = wp_specialchars( $widget_many_instance['something'] );
1496 $options[$widget_number] = array( 'something' => $something ); // Even simple widgets should store stuff in array, rather than in scalar
1499 update_option('widget_text', $options);
1501 $updated = true; // So that we don't go through this more than once
1505 // Here we echo out the form
1506 if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
1510 $something = attribute_escape($options[$number]['something']);
1513 // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
1514 // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
1517 <input class="widefat" id="widget-many-something-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][something]" type="text" value="<?php echo $data; ?>" />
1518 <input type="hidden" id="widget-many-submit-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][submit]" value="1" />
1523 // Registers each instance of our widget on startup
1524 function widget_many_register() {
1525 if ( !$options = get_option('widget_many') )
1528 $widget_ops = array('classname' => 'widget_many', 'description' => __('Widget which allows multiple instances'));
1529 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
1532 $registered = false;
1533 foreach ( array_keys($options) as $o ) {
1534 // Old widgets can have null values for some reason
1535 if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
1538 // $id should look like {$id_base}-{$o}
1539 $id = "many-$o"; // Never never never translate an id
1541 wp_register_sidebar_widget( $id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ) );
1542 wp_register_widget_control( $id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ) );
1545 // If there are none, we register the widget's existance with a generic template
1546 if ( !$registered ) {
1547 wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
1548 wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
1552 // This is important
1553 add_action( 'widgets_init', 'widget_many_register' )