5 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_styles, $wp_registered_widget_defaults;
7 $wp_registered_sidebars = array();
8 $wp_registered_widgets = array();
9 $wp_registered_widget_controls = array();
10 $wp_registered_widget_styles = array();
11 $wp_register_widget_defaults = false;
13 /* Template tags & API functions */
15 function register_sidebars($number = 1, $args = array()) {
16 $number = (int) $number;
18 if ( is_string($args) )
19 parse_str($args, $args);
23 while ( $i <= $number ) {
26 $_args['name'] = isset($args['name']) ? $args['name'] : sprintf(__('Sidebar %d'), $i);
28 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
30 $_args['id'] = isset($args['id']) ? $args['id'] : "sidebar-$i";
31 register_sidebar($_args);
36 function register_sidebar($args = array()) {
37 global $wp_registered_sidebars;
39 if ( is_string($args) )
40 parse_str($args, $args);
42 $i = count($wp_registered_sidebars) + 1;
45 'name' => sprintf(__('Sidebar %d'), $i ),
47 'before_widget' => '<li id="%1$s" class="widget %2$s">',
48 'after_widget' => "</li>\n",
49 'before_title' => '<h2 class="widgettitle">',
50 'after_title' => "</h2>\n",
53 $sidebar = array_merge($defaults, $args);
55 $wp_registered_sidebars[$sidebar['id']] = $sidebar;
57 return $sidebar['id'];
60 function unregister_sidebar( $name ) {
61 global $wp_registered_sidebars;
63 if ( isset( $wp_registered_sidebars[$name] ) )
64 unset( $wp_registered_sidebars[$name] );
67 function register_sidebar_widget($name, $output_callback, $classname = '') {
69 if ( is_array($name) ) {
70 if ( count($name) == 3 )
71 $name = sprintf($name[0], $name[2]);
76 $id = sanitize_title($name);
78 if ( !empty($classname) && is_string($classname) )
79 $options['classname'] = $classname;
80 $params = array_slice(func_get_args(), 2);
81 $args = array($id, $name, $output_callback, $options);
82 if ( !empty($params) )
83 $args = array_merge($args, $params);
85 call_user_func_array('wp_register_sidebar_widget', $args);
88 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
90 global $wp_registered_widgets, $wp_register_widget_defaults;
92 $id = sanitize_title($id);
94 if ( empty($output_callback) ) {
95 unset($wp_registered_widgets[$id]);
99 $defaults = array('classname' => $output_callback);
100 $options = wp_parse_args($options, $defaults);
104 'callback' => $output_callback,
105 'params' => array_slice(func_get_args(), 4)
107 $widget = array_merge($widget, $options);
109 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || !$wp_register_widget_defaults) )
110 $wp_registered_widgets[$id] = $widget;
113 function unregister_sidebar_widget($id) {
114 return wp_unregister_sidebar_widget($id);
117 function wp_unregister_sidebar_widget($id) {
118 wp_register_sidebar_widget($id, '', '');
119 wp_unregister_widget_control($id);
122 function register_widget_control($name, $control_callback, $width = '', $height = '') {
124 if ( is_array($name) ) {
125 if ( count($name) == 3 )
126 $name = sprintf($name[0], $name[2]);
131 $id = sanitize_title($name);
133 if ( !empty($width) )
134 $options['width'] = $width;
135 if ( !empty($height) )
136 $options['height'] = $height;
137 $params = array_slice(func_get_args(), 4);
138 $args = array($id, $name, $control_callback, $options);
139 if ( !empty($params) )
140 $args = array_merge($args, $params);
142 call_user_func_array('wp_register_widget_control', $args);
145 function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
146 global $wp_registered_widget_controls, $wp_register_widget_defaults;
148 $id = sanitize_title($id);
150 if ( empty($control_callback) ) {
151 unset($wp_registered_widget_controls[$id]);
155 if ( isset($wp_registered_widget_controls[$id]) && $wp_register_widget_defaults )
158 $defaults = array('width' => 300, 'height' => 200);
159 $options = wp_parse_args($options, $defaults);
160 $options['width'] = (int) $options['width'];
161 $options['height'] = (int) $options['height'];
162 $options['width'] = $options['width'] > 90 ? $options['width'] + 60 : 360;
163 $options['height'] = $options['height'] > 60 ? $options['height'] + 40 : 240;
168 'callback' => $control_callback,
169 'params' => array_slice(func_get_args(), 4)
171 $widget = array_merge($widget, $options);
173 $wp_registered_widget_controls[$id] = $widget;
176 function unregister_widget_control($id) {
177 return wp_unregister_widget_control($id);
180 function wp_unregister_widget_control($id) {
181 return wp_register_widget_control($id, '', '');
184 function dynamic_sidebar($index = 1) {
185 global $wp_registered_sidebars, $wp_registered_widgets;
187 if ( is_int($index) ) {
188 $index = "sidebar-$index";
190 $index = sanitize_title($index);
191 foreach ( $wp_registered_sidebars as $key => $value ) {
192 if ( sanitize_title($value['name']) == $index ) {
199 $sidebars_widgets = wp_get_sidebars_widgets();
201 if ( empty($wp_registered_sidebars[$index]) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
204 $sidebar = $wp_registered_sidebars[$index];
207 foreach ( $sidebars_widgets[$index] as $id ) {
208 $callback = $wp_registered_widgets[$id]['callback'];
210 $params = array_merge(array($sidebar), (array) $wp_registered_widgets[$id]['params']);
212 // Substitute HTML id and class attributes into before_widget
214 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
215 if ( is_string($cn) )
216 $classname_ .= '_' . $cn;
217 elseif ( is_object($cn) )
218 $classname_ .= '_' . get_class($cn);
220 $classname_ = ltrim($classname_, '_');
221 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
223 if ( is_callable($callback) ) {
224 call_user_func_array($callback, $params);
232 function is_active_widget($callback) {
233 global $wp_registered_widgets;
235 $sidebars_widgets = wp_get_sidebars_widgets(false);
237 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
238 if ( is_array($widgets) ) foreach ( $widgets as $widget )
239 if ( $wp_registered_widgets[$widget]['callback'] == $callback )
245 function is_dynamic_sidebar() {
246 global $wp_registered_widgets, $wp_registered_sidebars;
247 $sidebars_widgets = get_option('sidebars_widgets');
248 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
249 if ( count($sidebars_widgets[$index]) ) {
250 foreach ( $sidebars_widgets[$index] as $widget )
251 if ( array_key_exists($widget, $wp_registered_widgets) )
258 /* Internal Functions */
260 function wp_get_sidebars_widgets($update = true) {
261 global $wp_registered_widgets, $wp_registered_sidebars;
263 $sidebars_widgets = get_option('sidebars_widgets');
264 $_sidebars_widgets = array();
266 if ( !isset($sidebars_widgets['array_version']) )
267 $sidebars_widgets['array_version'] = 1;
269 switch ( $sidebars_widgets['array_version'] ) {
271 foreach ( $sidebars_widgets as $index => $sidebar )
272 if ( is_array($sidebar) )
273 foreach ( $sidebar as $i => $name ) {
274 $id = strtolower($name);
275 if ( isset($wp_registered_widgets[$id]) ) {
276 $_sidebars_widgets[$index][$i] = $id;
279 $id = sanitize_title($name);
280 if ( isset($wp_registered_widgets[$id]) ) {
281 $_sidebars_widgets[$index][$i] = $id;
284 unset($_sidebars_widgets[$index][$i]);
286 $_sidebars_widgets['array_version'] = 2;
287 $sidebars_widgets = $_sidebars_widgets;
288 unset($_sidebars_widgets);
291 $sidebars = array_keys( $wp_registered_sidebars );
292 if ( !empty( $sidebars ) ) {
293 // Move the known-good ones first
294 foreach ( $sidebars as $id ) {
295 if ( array_key_exists( $id, $sidebars_widgets ) ) {
296 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
297 unset($sidebars_widgets[$id], $sidebars[$id]);
301 // Assign to each unmatched registered sidebar the first available orphan
302 unset( $sidebars_widgets[ 'array_version' ] );
303 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
304 $_sidebars_widgets[ $sidebar ] = $widgets;
306 $_sidebars_widgets['array_version'] = 3;
307 $sidebars_widgets = $_sidebars_widgets;
308 unset($_sidebars_widgets);
312 update_option('sidebars_widgets', $sidebars_widgets);
315 unset($sidebars_widgets['array_version']);
317 return $sidebars_widgets;
320 function wp_set_sidebars_widgets( $sidebars_widgets ) {
321 update_option( 'sidebars_widgets', $sidebars_widgets );
324 function wp_get_widget_defaults() {
325 global $wp_registered_sidebars;
329 foreach ( $wp_registered_sidebars as $index => $sidebar )
330 $defaults[$index] = array();
335 /* Default Widgets */
337 function wp_widget_pages( $args ) {
339 $options = get_option( 'widget_pages' );
341 $title = empty( $options['title'] ) ? __( 'Pages' ) : $options['title'];
342 $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
343 $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
345 if ( $sortby == 'menu_order' ) {
346 $sortby = 'menu_order, post_title';
349 $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
351 if ( !empty( $out ) ) {
353 <?php echo $before_widget; ?>
354 <?php echo $before_title . $title . $after_title; ?>
358 <?php echo $after_widget; ?>
363 function wp_widget_pages_control() {
364 $options = $newoptions = get_option('widget_pages');
365 if ( $_POST['pages-submit'] ) {
366 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
368 $sortby = stripslashes( $_POST['pages-sortby'] );
370 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
371 $newoptions['sortby'] = $sortby;
373 $newoptions['sortby'] = 'menu_order';
376 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
378 if ( $options != $newoptions ) {
379 $options = $newoptions;
380 update_option('widget_pages', $options);
382 $title = attribute_escape($options['title']);
383 $exclude = attribute_escape( $options['exclude'] );
385 <p><label for="pages-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p>
386 <p><label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
387 <select name="pages-sortby" id="pages-sortby">
388 <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
389 <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
390 <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
391 </select></label></p>
392 <p><label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" style="width: 180px;" /></label><br />
393 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small></p>
394 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
398 function wp_widget_links($args) {
399 extract($args, EXTR_SKIP);
400 wp_list_bookmarks(array(
401 'title_before' => $before_title, 'title_after' => $after_title,
402 'category_before' => $before_widget, 'category_after' => $after_widget,
403 'show_images' => true, 'class' => 'linkcat widget'
407 function wp_widget_search($args) {
410 <?php echo $before_widget; ?>
411 <form id="searchform" method="get" action="<?php bloginfo('home'); ?>">
413 <input type="text" name="s" id="s" size="15" /><br />
414 <input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" />
417 <?php echo $after_widget; ?>
421 function wp_widget_archives($args) {
423 $options = get_option('widget_archives');
424 $c = $options['count'] ? '1' : '0';
425 $d = $options['dropdown'] ? '1' : '0';
426 $title = empty($options['title']) ? __('Archives') : $options['title'];
429 echo $before_title . $title . $after_title;
433 <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>
438 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
446 function wp_widget_archives_control() {
447 $options = $newoptions = get_option('widget_archives');
448 if ( $_POST["archives-submit"] ) {
449 $newoptions['count'] = isset($_POST['archives-count']);
450 $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
451 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
453 if ( $options != $newoptions ) {
454 $options = $newoptions;
455 update_option('widget_archives', $options);
457 $count = $options['count'] ? 'checked="checked"' : '';
458 $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
459 $title = attribute_escape($options['title']);
461 <p><label for="archives-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
462 <p style="text-align:right;margin-right:40px;"><label for="archives-count"><?php _e('Show post counts'); ?> <input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /></label></p>
463 <p style="text-align:right;margin-right:40px;"><label for="archives-dropdown"><?php _e('Display as a drop down'); ?> <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /></label></p>
464 <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
468 function wp_widget_meta($args) {
470 $options = get_option('widget_meta');
471 $title = empty($options['title']) ? __('Meta') : $options['title'];
473 <?php echo $before_widget; ?>
474 <?php echo $before_title . $title . $after_title; ?>
476 <?php wp_register(); ?>
477 <li><?php wp_loginout(); ?></li>
478 <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>
479 <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>
480 <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>
483 <?php echo $after_widget; ?>
486 function wp_widget_meta_control() {
487 $options = $newoptions = get_option('widget_meta');
488 if ( $_POST["meta-submit"] ) {
489 $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
491 if ( $options != $newoptions ) {
492 $options = $newoptions;
493 update_option('widget_meta', $options);
495 $title = attribute_escape($options['title']);
497 <p><label for="meta-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="meta-title" name="meta-title" type="text" value="<?php echo $title; ?>" /></label></p>
498 <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
502 function wp_widget_calendar($args) {
504 $options = get_option('widget_calendar');
505 $title = $options['title'];
508 echo $before_widget . $before_title . $title . $after_title;
509 echo '<div id="calendar_wrap">';
514 function wp_widget_calendar_control() {
515 $options = $newoptions = get_option('widget_calendar');
516 if ( $_POST["calendar-submit"] ) {
517 $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
519 if ( $options != $newoptions ) {
520 $options = $newoptions;
521 update_option('widget_calendar', $options);
523 $title = attribute_escape($options['title']);
525 <p><label for="calendar-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="calendar-title" name="calendar-title" type="text" value="<?php echo $title; ?>" /></label></p>
526 <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
530 function wp_widget_text($args, $number = 1) {
532 $options = get_option('widget_text');
533 $title = $options[$number]['title'];
534 $text = apply_filters( 'widget_text', $options[$number]['text'] );
536 <?php echo $before_widget; ?>
537 <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
538 <div class="textwidget"><?php echo $text; ?></div>
539 <?php echo $after_widget; ?>
543 function wp_widget_text_control($number) {
544 $options = $newoptions = get_option('widget_text');
545 if ( !is_array($options) )
546 $options = $newoptions = array();
547 if ( $_POST["text-submit-$number"] ) {
548 $newoptions[$number]['title'] = strip_tags(stripslashes($_POST["text-title-$number"]));
549 $newoptions[$number]['text'] = stripslashes($_POST["text-text-$number"]);
550 if ( !current_user_can('unfiltered_html') )
551 $newoptions[$number]['text'] = stripslashes(wp_filter_post_kses($newoptions[$number]['text']));
553 if ( $options != $newoptions ) {
554 $options = $newoptions;
555 update_option('widget_text', $options);
557 $title = attribute_escape($options[$number]['title']);
558 $text = format_to_edit($options[$number]['text']);
560 <input style="width: 450px;" id="text-title-<?php echo $number; ?>" name="text-title-<?php echo $number; ?>" type="text" value="<?php echo $title; ?>" />
561 <textarea style="width: 450px; height: 280px;" id="text-text-<?php echo $number; ?>" name="text-text-<?php echo $number; ?>"><?php echo $text; ?></textarea>
562 <input type="hidden" id="text-submit-<?php echo "$number"; ?>" name="text-submit-<?php echo "$number"; ?>" value="1" />
566 function wp_widget_text_setup() {
567 $options = $newoptions = get_option('widget_text');
568 if ( isset($_POST['text-number-submit']) ) {
569 $number = (int) $_POST['text-number'];
570 if ( $number > 9 ) $number = 9;
571 if ( $number < 1 ) $number = 1;
572 $newoptions['number'] = $number;
574 if ( $options != $newoptions ) {
575 $options = $newoptions;
576 update_option('widget_text', $options);
577 wp_widget_text_register($options['number']);
581 function wp_widget_text_page() {
582 $options = $newoptions = get_option('widget_text');
586 <h2><?php _e('Text Widgets'); ?></h2>
587 <p style="line-height: 30px;"><?php _e('How many text widgets would you like?'); ?>
588 <select id="text-number" name="text-number" value="<?php echo $options['number']; ?>">
589 <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
591 <span class="submit"><input type="submit" name="text-number-submit" id="text-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p>
597 function wp_widget_text_register() {
598 $options = get_option('widget_text');
599 $number = $options['number'];
600 if ( $number < 1 ) $number = 1;
601 if ( $number > 9 ) $number = 9;
602 $dims = array('width' => 460, 'height' => 350);
603 $class = array('classname' => 'widget_text');
604 for ($i = 1; $i <= 9; $i++) {
605 $name = sprintf(__('Text %d'), $i);
606 $id = "text-$i"; // Never never never translate an id
607 wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_text' : /* unregister */ '', $class, $i);
608 wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_text_control' : /* unregister */ '', $dims, $i);
610 add_action('sidebar_admin_setup', 'wp_widget_text_setup');
611 add_action('sidebar_admin_page', 'wp_widget_text_page');
614 function wp_widget_categories($args, $number = 1) {
616 $options = get_option('widget_categories');
618 $c = $options[$number]['count'] ? '1' : '0';
619 $h = $options[$number]['hierarchical'] ? '1' : '0';
620 $d = $options[$number]['dropdown'] ? '1' : '0';
622 $title = empty($options[$number]['title']) ? __('Categories') : $options[$number]['title'];
625 echo $before_title . $title . $after_title;
627 $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}";
630 wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category'));
633 <script lang='javascript'><!--
634 var dropdown = document.getElementById("cat");
635 function onCatChange() {
636 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
637 location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
640 dropdown.onchange = onCatChange;
647 <?php wp_list_categories($cat_args . '&title_li='); ?>
655 function wp_widget_categories_control( $number ) {
656 $options = $newoptions = get_option('widget_categories');
658 if ( !is_array( $options ) ) {
659 $options = $newoptions = get_option( 'widget_categories' );
662 if ( $_POST['categories-submit-' . $number] ) {
663 $newoptions[$number]['count'] = isset($_POST['categories-count-' . $number]);
664 $newoptions[$number]['hierarchical'] = isset($_POST['categories-hierarchical-' . $number]);
665 $newoptions[$number]['dropdown'] = isset($_POST['categories-dropdown-' . $number]);
666 $newoptions[$number]['title'] = strip_tags(stripslashes($_POST['categories-title-' . $number]));
669 if ( $options != $newoptions ) {
670 $options = $newoptions;
671 update_option('widget_categories', $options);
674 $title = attribute_escape( $options[$number]['title'] );
676 <p><label for="categories-title-<?php echo $number; ?>">
677 <?php _e( 'Title:' ); ?> <input style="width:300px" id="categories-title-<?php echo $number; ?>" name="categories-title-<?php echo $number; ?>" type="text" value="<?php echo $title; ?>" />
680 <p><label for="categories-dropdown-<?php echo $number; ?>">
681 <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="categories-dropdown-<?php echo $number; ?>"<?php echo $options[$number]['dropdown'] ? ' checked="checked"' : ''; ?> /> <?php _e( 'Show as dropdown' ); ?>
684 <p><label for="categories-count-<?php echo $number; ?>">
685 <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="categories-count-<?php echo $number; ?>"<?php echo $options[$number]['count'] ? ' checked="checked"' : ''; ?> /> <?php _e( 'Show post counts' ); ?>
688 <p><label for="categories-hierarchical-<?php echo $number; ?>">
689 <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="categories-hierarchical-<?php echo $number; ?>"<?php echo $options[$number]['hierarchical'] ? ' checked="checked"' : ''; ?> /> <?php _e( 'Show hierarchy' ); ?>
692 <input type="hidden" id="categories-submit-<?php echo $number; ?>" name="categories-submit-<?php echo $number; ?>" value="1" />
696 function wp_widget_categories_setup() {
697 $options = $newoptions = get_option( 'widget_categories' );
699 if ( isset( $_POST['categories-number-submit'] ) ) {
700 $number = (int) $_POST['categories-number'];
704 } elseif ( $number < 1 ) {
708 $newoptions['number'] = $number;
711 if ( $newoptions != $options ) {
712 $options = $newoptions;
713 update_option( 'widget_categories', $options );
714 wp_widget_categories_register( $options['number'] );
718 function wp_widget_categories_page() {
719 $options = get_option( 'widget_categories' );
723 <h2><?php _e( 'Categories Widgets' ); ?></h2>
724 <p style="line-height: 30px;"><?php _e( 'How many categories widgets would you like?' ); ?>
725 <select id="categories-number" name="categories-number" value="<?php echo attribute_escape( $options['number'] ); ?>">
727 for ( $i = 1; $i < 10; $i++ ) {
728 echo '<option value="' . $i . '"' . ( $i == $options['number'] ? ' selected="selected"' : '' ) . '>' . $i . "</option>\n";
732 <span class="submit">
733 <input type="submit" value="<?php echo attribute_escape( __( 'Save' ) ); ?>" id="categories-number-submit" name="categories-number-submit" />
741 function wp_widget_categories_upgrade() {
742 $options = get_option( 'widget_categories' );
744 $newoptions = array( 'number' => 1, 1 => $options );
746 update_option( 'widget_categories', $newoptions );
748 $sidebars_widgets = get_option( 'sidebars_widgets' );
749 if ( is_array( $sidebars_widgets ) ) {
750 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
751 if ( is_array( $widgets ) ) {
752 foreach ( $widgets as $widget )
753 $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
755 $new_widgets[$sidebar] = $widgets;
758 if ( $new_widgets != $sidebars_widgets )
759 update_option( 'sidebars_widgets', $new_widgets );
762 if ( isset( $_POST['categories-submit'] ) ) {
763 $_POST['categories-submit-1'] = $_POST['categories-submit'];
764 $_POST['categories-count-1'] = $_POST['categories-count'];
765 $_POST['categories-hierarchical-1'] = $_POST['categories-hierarchical'];
766 $_POST['categories-dropdown-1'] = $_POST['categories-dropdown'];
767 $_POST['categories-title-1'] = $_POST['categories-title'];
768 foreach ( $_POST as $k => $v )
769 if ( substr($k, -5) == 'order' )
770 $_POST[$k] = str_replace('categories', 'categories-1', $v);
776 function wp_widget_categories_register() {
777 $options = get_option( 'widget_categories' );
778 if ( !isset($options['number']) )
779 $options = wp_widget_categories_upgrade();
780 $number = (int) $options['number'];
784 } elseif ( $number < 1 ) {
788 $dims = array( 'width' => 350, 'height' => 170 );
789 $class = array( 'classname' => 'widget_categories' );
791 for ( $i = 1; $i <= 9; $i++ ) {
792 $name = sprintf( __( 'Categories %d' ), $i );
793 $id = 'categories-' . $i;
795 $widget_callback = ( $i <= $number ) ? 'wp_widget_categories' : '';
796 $control_callback = ( $i <= $number ) ? 'wp_widget_categories_control' : '';
798 wp_register_sidebar_widget( $id, $name, $widget_callback, $class, $i );
799 wp_register_widget_control( $id, $name, $control_callback, $dims, $i );
802 add_action( 'sidebar_admin_setup', 'wp_widget_categories_setup' );
803 add_action( 'sidebar_admin_page', 'wp_widget_categories_page' );
806 function wp_widget_recent_entries($args) {
807 if ( $output = wp_cache_get('widget_recent_entries') )
808 return print($output);
812 $options = get_option('widget_recent_entries');
813 $title = empty($options['title']) ? __('Recent Posts') : $options['title'];
814 if ( !$number = (int) $options['number'] )
816 else if ( $number < 1 )
818 else if ( $number > 15 )
821 $r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0&post_status=publish");
822 if ($r->have_posts()) :
824 <?php echo $before_widget; ?>
825 <?php echo $before_title . $title . $after_title; ?>
827 <?php while ($r->have_posts()) : $r->the_post(); ?>
828 <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
831 <?php echo $after_widget; ?>
834 wp_cache_add('widget_recent_entries', ob_get_flush());
837 function wp_flush_widget_recent_entries() {
838 wp_cache_delete('widget_recent_entries');
841 add_action('save_post', 'wp_flush_widget_recent_entries');
842 add_action('deleted_post', 'wp_flush_widget_recent_entries');
844 function wp_widget_recent_entries_control() {
845 $options = $newoptions = get_option('widget_recent_entries');
846 if ( $_POST["recent-entries-submit"] ) {
847 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
848 $newoptions['number'] = (int) $_POST["recent-entries-number"];
850 if ( $options != $newoptions ) {
851 $options = $newoptions;
852 update_option('widget_recent_entries', $options);
853 wp_flush_widget_recent_entries();
855 $title = attribute_escape($options['title']);
856 if ( !$number = (int) $options['number'] )
859 <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
860 <p><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> <?php _e('(at most 15)'); ?></p>
861 <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
865 function wp_widget_recent_comments($args) {
866 global $wpdb, $comments, $comment;
867 extract($args, EXTR_SKIP);
868 $options = get_option('widget_recent_comments');
869 $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
870 if ( !$number = (int) $options['number'] )
872 else if ( $number < 1 )
874 else if ( $number > 15 )
877 if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
878 $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");
879 wp_cache_add( 'recent_comments', $comments, 'widget' );
883 <?php echo $before_widget; ?>
884 <?php echo $before_title . $title . $after_title; ?>
885 <ul id="recentcomments"><?php
886 if ( $comments ) : foreach ($comments as $comment) :
887 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>';
888 endforeach; endif;?></ul>
889 <?php echo $after_widget; ?>
893 function wp_delete_recent_comments_cache() {
894 wp_cache_delete( 'recent_comments', 'widget' );
896 add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
897 add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
899 function wp_widget_recent_comments_control() {
900 $options = $newoptions = get_option('widget_recent_comments');
901 if ( $_POST["recent-comments-submit"] ) {
902 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
903 $newoptions['number'] = (int) $_POST["recent-comments-number"];
905 if ( $options != $newoptions ) {
906 $options = $newoptions;
907 update_option('widget_recent_comments', $options);
908 wp_delete_recent_comments_cache();
910 $title = attribute_escape($options['title']);
911 if ( !$number = (int) $options['number'] )
914 <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
915 <p><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> <?php _e('(at most 15)'); ?></p>
916 <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
920 function wp_widget_recent_comments_style() {
922 <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
926 function wp_widget_recent_comments_register() {
927 $dims = array('width' => 320, 'height' => 90);
928 $class = array('classname' => 'widget_recent_comments');
929 wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $class);
930 wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control', $dims);
932 if ( is_active_widget('wp_widget_recent_comments') )
933 add_action('wp_head', 'wp_widget_recent_comments_style');
936 function wp_widget_rss($args, $number = 1) {
937 require_once(ABSPATH . WPINC . '/rss.php');
939 $options = get_option('widget_rss');
940 if ( isset($options['error']) && $options['error'] )
942 $num_items = (int) $options[$number]['items'];
943 $show_summary = $options[$number]['show_summary'];
944 if ( empty($num_items) || $num_items < 1 || $num_items > 10 ) $num_items = 10;
945 $url = $options[$number]['url'];
946 while ( strstr($url, 'http') != $url )
947 $url = substr($url, 1);
950 $rss = fetch_rss($url);
951 $link = clean_url(strip_tags($rss->channel['link']));
952 while ( strstr($link, 'http') != $link )
953 $link = substr($link, 1);
954 $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
955 $title = $options[$number]['title'];
957 $title = htmlentities(strip_tags($rss->channel['title']));
961 $title = __('Unknown Feed');
962 $url = clean_url(strip_tags($url));
963 if ( file_exists(dirname(__FILE__) . '/rss.png') )
964 $icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png';
966 $icon = get_option('siteurl').'/wp-includes/images/rss.png';
967 $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>";
969 <?php echo $before_widget; ?>
970 <?php $title ? print($before_title . $title . $after_title) : null; ?>
972 if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
973 $rss->items = array_slice($rss->items, 0, $num_items);
975 foreach ($rss->items as $item ) {
976 while ( strstr($item['link'], 'http') != $item['link'] )
977 $item['link'] = substr($item['link'], 1);
978 $link = clean_url(strip_tags($item['link']));
979 $title = attribute_escape(strip_tags($item['title']));
981 $title = __('Untitled');
983 if ( $show_summary ) {
984 $summary = '<div class="rssSummary">' . $item['description'] . '</div>';
986 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
987 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
990 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>$summary</li>";
994 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
1000 function wp_widget_rss_control($number) {
1001 $options = $newoptions = get_option('widget_rss');
1002 if ( $_POST["rss-submit-$number"] ) {
1003 $newoptions[$number]['items'] = (int) $_POST["rss-items-$number"];
1004 $url = sanitize_url(strip_tags(stripslashes($_POST["rss-url-$number"])));
1005 $newoptions[$number]['title'] = trim(strip_tags(stripslashes($_POST["rss-title-$number"])));
1006 if ( $url !== $options[$number]['url'] ) {
1007 require_once(ABSPATH . WPINC . '/rss.php');
1008 $rss = fetch_rss($url);
1009 if ( is_object($rss) ) {
1010 $newoptions[$number]['url'] = $url;
1011 $newoptions[$number]['error'] = false;
1013 $newoptions[$number]['error'] = true;
1014 $newoptions[$number]['url'] = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
1015 $error = sprintf(__('Error in RSS %1$d: %2$s'), $number, $newoptions[$number]['error']);
1019 if ( $options != $newoptions ) {
1020 $options = $newoptions;
1021 update_option('widget_rss', $options);
1023 $url = attribute_escape($options[$number]['url']);
1024 $items = (int) $options[$number]['items'];
1025 $title = attribute_escape($options[$number]['title']);
1026 if ( empty($items) || $items < 1 ) $items = 10;
1028 <p style="text-align:center;"><?php _e('Enter the RSS feed URL here:'); ?></p>
1029 <input style="width: 400px;" id="rss-url-<?php echo "$number"; ?>" name="rss-url-<?php echo "$number"; ?>" type="text" value="<?php echo $url; ?>" />
1030 <p style="text-align:center;"><?php _e('Give the feed a title (optional):'); ?></p>
1031 <input style="width: 400px;" id="rss-title-<?php echo "$number"; ?>" name="rss-title-<?php echo "$number"; ?>" type="text" value="<?php echo $title; ?>" />
1032 <p style="text-align:center; line-height: 30px;"><?php _e('How many items would you like to display?'); ?> <select id="rss-items-<?php echo $number; ?>" name="rss-items-<?php echo $number; ?>"><?php for ( $i = 1; $i <= 10; ++$i ) echo "<option value='$i' ".($items==$i ? "selected='selected'" : '').">$i</option>"; ?></select></p>
1033 <input type="hidden" id="rss-submit-<?php echo "$number"; ?>" name="rss-submit-<?php echo "$number"; ?>" value="1" />
1037 function wp_widget_rss_setup() {
1038 $options = $newoptions = get_option('widget_rss');
1039 if ( isset($_POST['rss-number-submit']) ) {
1040 $number = (int) $_POST['rss-number'];
1041 if ( $number > 9 ) $number = 9;
1042 if ( $number < 1 ) $number = 1;
1043 $newoptions['number'] = $number;
1045 if ( $options != $newoptions ) {
1046 $options = $newoptions;
1047 update_option('widget_rss', $options);
1048 wp_widget_rss_register($options['number']);
1052 function wp_widget_rss_page() {
1053 $options = $newoptions = get_option('widget_rss');
1056 <form method="POST">
1057 <h2><?php _e('RSS Feed Widgets'); ?></h2>
1058 <p style="line-height: 30px;"><?php _e('How many RSS widgets would you like?'); ?>
1059 <select id="rss-number" name="rss-number" value="<?php echo $options['number']; ?>">
1060 <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
1062 <span class="submit"><input type="submit" name="rss-number-submit" id="rss-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p>
1068 function wp_widget_rss_register() {
1069 $options = get_option('widget_rss');
1070 $number = $options['number'];
1071 if ( $number < 1 ) $number = 1;
1072 if ( $number > 9 ) $number = 9;
1073 $dims = array('width' => 410, 'height' => 200);
1074 $class = array('classname' => 'widget_rss');
1075 for ($i = 1; $i <= 9; $i++) {
1076 $name = sprintf(__('RSS %d'), $i);
1077 $id = "rss-$i"; // Never never never translate an id
1078 wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_rss' : /* unregister */ '', $class, $i);
1079 wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_rss_control' : /* unregister */ '', $dims, $i);
1081 add_action('sidebar_admin_setup', 'wp_widget_rss_setup');
1082 add_action('sidebar_admin_page', 'wp_widget_rss_page');
1085 function wp_widget_tag_cloud($args) {
1087 $options = get_option('widget_tag_cloud');
1088 $title = empty($options['title']) ? __('Tags') : $options['title'];
1090 echo $before_widget;
1091 echo $before_title . $title . $after_title;
1096 function wp_widget_tag_cloud_control() {
1097 $options = $newoptions = get_option('widget_tag_cloud');
1099 if ( $_POST['tag-cloud-submit'] ) {
1100 $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
1103 if ( $options != $newoptions ) {
1104 $options = $newoptions;
1105 update_option('widget_tag_cloud', $options);
1108 $title = attribute_escape( $options['title'] );
1110 <p><label for="tag-cloud-title">
1111 <?php _e('Title:') ?> <input type="text" style="width:300px" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
1113 <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
1117 function wp_widgets_init() {
1118 if ( !is_blog_installed() )
1121 $GLOBALS['wp_register_widget_defaults'] = true;
1123 $dims90 = array( 'height' => 90, 'width' => 300 );
1124 $dims100 = array( 'height' => 100, 'width' => 300 );
1125 $dims150 = array( 'height' => 150, 'width' => 300 );
1127 $class = array('classname' => 'widget_pages');
1128 wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $class);
1129 wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control', $dims150);
1131 $class['classname'] = 'widget_calendar';
1132 wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $class);
1133 wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control', $dims90);
1135 $class['classname'] = 'widget_archives';
1136 wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $class);
1137 wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control', $dims100);
1139 $class['classname'] = 'widget_links';
1140 wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $class);
1142 $class['classname'] = 'widget_meta';
1143 wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $class);
1144 wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control', $dims90);
1146 $class['classname'] = 'widget_search';
1147 wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $class);
1149 $class['classname'] = 'widget_recent_entries';
1150 wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $class);
1151 wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control', $dims90);
1153 $class['classname'] = 'widget_tag_cloud';
1154 wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $class);
1155 wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control', 'width=300&height=160');
1157 wp_widget_categories_register();
1158 wp_widget_text_register();
1159 wp_widget_rss_register();
1160 wp_widget_recent_comments_register();
1162 $GLOBALS['wp_register_widget_defaults'] = false;
1164 do_action('widgets_init');
1167 add_action('init', 'wp_widgets_init', 1);