14 class WP_Widget_Pages extends WP_Widget {
16 function WP_Widget_Pages() {
17 $widget_ops = array('classname' => 'widget_pages', 'description' => __( 'Your site’s WordPress Pages') );
18 $this->WP_Widget('pages', __('Pages'), $widget_ops);
21 function widget( $args, $instance ) {
24 $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base);
25 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
26 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
28 if ( $sortby == 'menu_order' )
29 $sortby = 'menu_order, post_title';
31 $out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) );
33 if ( !empty( $out ) ) {
36 echo $before_title . $title . $after_title;
46 function update( $new_instance, $old_instance ) {
47 $instance = $old_instance;
48 $instance['title'] = strip_tags($new_instance['title']);
49 if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
50 $instance['sortby'] = $new_instance['sortby'];
52 $instance['sortby'] = 'menu_order';
55 $instance['exclude'] = strip_tags( $new_instance['exclude'] );
60 function form( $instance ) {
62 $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
63 $title = esc_attr( $instance['title'] );
64 $exclude = esc_attr( $instance['exclude'] );
66 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
68 <label for="<?php echo $this->get_field_id('sortby'); ?>"><?php _e( 'Sort by:' ); ?></label>
69 <select name="<?php echo $this->get_field_name('sortby'); ?>" id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat">
70 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
71 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
72 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
76 <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" />
78 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
90 class WP_Widget_Links extends WP_Widget {
92 function WP_Widget_Links() {
93 $widget_ops = array('description' => __( "Your blogroll" ) );
94 $this->WP_Widget('links', __('Links'), $widget_ops);
97 function widget( $args, $instance ) {
98 extract($args, EXTR_SKIP);
100 $show_description = isset($instance['description']) ? $instance['description'] : false;
101 $show_name = isset($instance['name']) ? $instance['name'] : false;
102 $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
103 $show_images = isset($instance['images']) ? $instance['images'] : true;
104 $category = isset($instance['category']) ? $instance['category'] : false;
106 if ( is_admin() && !$category ) {
107 // Display All Links widget as such in the widgets screen
108 echo $before_widget . $before_title. __('All Links') . $after_title . $after_widget;
112 $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
113 wp_list_bookmarks(apply_filters('widget_links_args', array(
114 'title_before' => $before_title, 'title_after' => $after_title,
115 'category_before' => $before_widget, 'category_after' => $after_widget,
116 'show_images' => $show_images, 'show_description' => $show_description,
117 'show_name' => $show_name, 'show_rating' => $show_rating,
118 'category' => $category, 'class' => 'linkcat widget'
122 function update( $new_instance, $old_instance ) {
123 $new_instance = (array) $new_instance;
124 $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0);
125 foreach ( $instance as $field => $val ) {
126 if ( isset($new_instance[$field]) )
127 $instance[$field] = 1;
129 $instance['category'] = intval($new_instance['category']);
134 function form( $instance ) {
137 $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false ) );
138 $link_cats = get_terms( 'link_category');
141 <label for="<?php echo $this->get_field_id('category'); ?>" class="screen-reader-text"><?php _e('Select Link Category'); ?></label>
142 <select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
143 <option value=""><?php _e('All Links'); ?></option>
145 foreach ( $link_cats as $link_cat ) {
146 echo '<option value="' . intval($link_cat->term_id) . '"'
147 . ( $link_cat->term_id == $instance['category'] ? ' selected="selected"' : '' )
148 . '>' . $link_cat->name . "</option>\n";
153 <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" />
154 <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
155 <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" />
156 <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
157 <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
158 <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
159 <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
160 <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
167 * Search widget class
171 class WP_Widget_Search extends WP_Widget {
173 function WP_Widget_Search() {
174 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your site") );
175 $this->WP_Widget('search', __('Search'), $widget_ops);
178 function widget( $args, $instance ) {
180 $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
184 echo $before_title . $title . $after_title;
186 // Use current theme search form if it exists
192 function form( $instance ) {
193 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
194 $title = $instance['title'];
196 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
200 function update( $new_instance, $old_instance ) {
201 $instance = $old_instance;
202 $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
203 $instance['title'] = strip_tags($new_instance['title']);
210 * Archives widget class
214 class WP_Widget_Archives extends WP_Widget {
216 function WP_Widget_Archives() {
217 $widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your site’s posts') );
218 $this->WP_Widget('archives', __('Archives'), $widget_ops);
221 function widget( $args, $instance ) {
223 $c = $instance['count'] ? '1' : '0';
224 $d = $instance['dropdown'] ? '1' : '0';
225 $title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title'], $instance, $this->id_base);
229 echo $before_title . $title . $after_title;
233 <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Month')); ?></option> <?php wp_get_archives(apply_filters('widget_archives_dropdown_args', array('type' => 'monthly', 'format' => 'option', 'show_post_count' => $c))); ?> </select>
238 <?php wp_get_archives(apply_filters('widget_archives_args', array('type' => 'monthly', 'show_post_count' => $c))); ?>
246 function update( $new_instance, $old_instance ) {
247 $instance = $old_instance;
248 $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
249 $instance['title'] = strip_tags($new_instance['title']);
250 $instance['count'] = $new_instance['count'] ? 1 : 0;
251 $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
256 function form( $instance ) {
257 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
258 $title = strip_tags($instance['title']);
259 $count = $instance['count'] ? 'checked="checked"' : '';
260 $dropdown = $instance['dropdown'] ? 'checked="checked"' : '';
262 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
264 <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
266 <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
275 * Displays log in/out, RSS feed links, etc.
279 class WP_Widget_Meta extends WP_Widget {
281 function WP_Widget_Meta() {
282 $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
283 $this->WP_Widget('meta', __('Meta'), $widget_ops);
286 function widget( $args, $instance ) {
288 $title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base);
292 echo $before_title . $title . $after_title;
295 <?php wp_register(); ?>
296 <li><?php wp_loginout(); ?></li>
297 <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
298 <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
299 <li><a href="http://wordpress.org/" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
306 function update( $new_instance, $old_instance ) {
307 $instance = $old_instance;
308 $instance['title'] = strip_tags($new_instance['title']);
313 function form( $instance ) {
314 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
315 $title = strip_tags($instance['title']);
317 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
323 * Calendar widget class
327 class WP_Widget_Calendar extends WP_Widget {
329 function WP_Widget_Calendar() {
330 $widget_ops = array('classname' => 'widget_calendar', 'description' => __( 'A calendar of your site’s posts') );
331 $this->WP_Widget('calendar', __('Calendar'), $widget_ops);
334 function widget( $args, $instance ) {
336 $title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title'], $instance, $this->id_base);
339 echo $before_title . $title . $after_title;
340 echo '<div id="calendar_wrap">';
346 function update( $new_instance, $old_instance ) {
347 $instance = $old_instance;
348 $instance['title'] = strip_tags($new_instance['title']);
353 function form( $instance ) {
354 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
355 $title = strip_tags($instance['title']);
357 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
358 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
368 class WP_Widget_Text extends WP_Widget {
370 function WP_Widget_Text() {
371 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
372 $control_ops = array('width' => 400, 'height' => 350);
373 $this->WP_Widget('text', __('Text'), $widget_ops, $control_ops);
376 function widget( $args, $instance ) {
378 $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
379 $text = apply_filters( 'widget_text', $instance['text'], $instance );
381 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
382 <div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
387 function update( $new_instance, $old_instance ) {
388 $instance = $old_instance;
389 $instance['title'] = strip_tags($new_instance['title']);
390 if ( current_user_can('unfiltered_html') )
391 $instance['text'] = $new_instance['text'];
393 $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
394 $instance['filter'] = isset($new_instance['filter']);
398 function form( $instance ) {
399 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
400 $title = strip_tags($instance['title']);
401 $text = esc_textarea($instance['text']);
403 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
404 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
406 <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
408 <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
414 * Categories widget class
418 class WP_Widget_Categories extends WP_Widget {
420 function WP_Widget_Categories() {
421 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
422 $this->WP_Widget('categories', __('Categories'), $widget_ops);
425 function widget( $args, $instance ) {
428 $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base);
429 $c = $instance['count'] ? '1' : '0';
430 $h = $instance['hierarchical'] ? '1' : '0';
431 $d = $instance['dropdown'] ? '1' : '0';
435 echo $before_title . $title . $after_title;
437 $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
440 $cat_args['show_option_none'] = __('Select Category');
441 wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
444 <script type='text/javascript'>
446 var dropdown = document.getElementById("cat");
447 function onCatChange() {
448 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
449 location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
452 dropdown.onchange = onCatChange;
461 $cat_args['title_li'] = '';
462 wp_list_categories(apply_filters('widget_categories_args', $cat_args));
471 function update( $new_instance, $old_instance ) {
472 $instance = $old_instance;
473 $instance['title'] = strip_tags($new_instance['title']);
474 $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
475 $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
476 $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
481 function form( $instance ) {
483 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
484 $title = esc_attr( $instance['title'] );
485 $count = isset($instance['count']) ? (bool) $instance['count'] :false;
486 $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
487 $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
489 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
490 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
492 <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
493 <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
495 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
496 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
498 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
499 <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
506 * Recent_Posts widget class
510 class WP_Widget_Recent_Posts extends WP_Widget {
512 function WP_Widget_Recent_Posts() {
513 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
514 $this->WP_Widget('recent-posts', __('Recent Posts'), $widget_ops);
515 $this->alt_option_name = 'widget_recent_entries';
517 add_action( 'save_post', array(&$this, 'flush_widget_cache') );
518 add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
519 add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
522 function widget($args, $instance) {
523 $cache = wp_cache_get('widget_recent_posts', 'widget');
525 if ( !is_array($cache) )
528 if ( isset($cache[$args['widget_id']]) ) {
529 echo $cache[$args['widget_id']];
536 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
537 if ( ! $number = absint( $instance['number'] ) )
540 $r = new WP_Query(array('posts_per_page' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => true));
541 if ($r->have_posts()) :
543 <?php echo $before_widget; ?>
544 <?php if ( $title ) echo $before_title . $title . $after_title; ?>
546 <?php while ($r->have_posts()) : $r->the_post(); ?>
547 <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
550 <?php echo $after_widget; ?>
552 // Reset the global $the_post as this query will have stomped on it
557 $cache[$args['widget_id']] = ob_get_flush();
558 wp_cache_set('widget_recent_posts', $cache, 'widget');
561 function update( $new_instance, $old_instance ) {
562 $instance = $old_instance;
563 $instance['title'] = strip_tags($new_instance['title']);
564 $instance['number'] = (int) $new_instance['number'];
565 $this->flush_widget_cache();
567 $alloptions = wp_cache_get( 'alloptions', 'options' );
568 if ( isset($alloptions['widget_recent_entries']) )
569 delete_option('widget_recent_entries');
574 function flush_widget_cache() {
575 wp_cache_delete('widget_recent_posts', 'widget');
578 function form( $instance ) {
579 $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
580 $number = isset($instance['number']) ? absint($instance['number']) : 5;
582 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
583 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
585 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
586 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
592 * Recent_Comments widget class
596 class WP_Widget_Recent_Comments extends WP_Widget {
598 function WP_Widget_Recent_Comments() {
599 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
600 $this->WP_Widget('recent-comments', __('Recent Comments'), $widget_ops);
601 $this->alt_option_name = 'widget_recent_comments';
603 if ( is_active_widget(false, false, $this->id_base) )
604 add_action( 'wp_head', array(&$this, 'recent_comments_style') );
606 add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
607 add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
610 function recent_comments_style() {
611 if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
612 || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
615 <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
619 function flush_widget_cache() {
620 wp_cache_delete('widget_recent_comments', 'widget');
623 function widget( $args, $instance ) {
624 global $comments, $comment;
626 $cache = wp_cache_get('widget_recent_comments', 'widget');
628 if ( ! is_array( $cache ) )
631 if ( isset( $cache[$args['widget_id']] ) ) {
632 echo $cache[$args['widget_id']];
636 extract($args, EXTR_SKIP);
638 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']);
640 if ( ! $number = absint( $instance['number'] ) )
643 $comments = get_comments( array( 'number' => $number, 'status' => 'approve' ) );
644 $output .= $before_widget;
646 $output .= $before_title . $title . $after_title;
648 $output .= '<ul id="recentcomments">';
650 foreach ( (array) $comments as $comment) {
651 $output .= '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
655 $output .= $after_widget;
658 $cache[$args['widget_id']] = $output;
659 wp_cache_set('widget_recent_comments', $cache, 'widget');
662 function update( $new_instance, $old_instance ) {
663 $instance = $old_instance;
664 $instance['title'] = strip_tags($new_instance['title']);
665 $instance['number'] = absint( $new_instance['number'] );
666 $this->flush_widget_cache();
668 $alloptions = wp_cache_get( 'alloptions', 'options' );
669 if ( isset($alloptions['widget_recent_comments']) )
670 delete_option('widget_recent_comments');
675 function form( $instance ) {
676 $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
677 $number = isset($instance['number']) ? absint($instance['number']) : 5;
679 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
680 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
682 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
683 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
693 class WP_Widget_RSS extends WP_Widget {
695 function WP_Widget_RSS() {
696 $widget_ops = array( 'description' => __('Entries from any RSS or Atom feed') );
697 $control_ops = array( 'width' => 400, 'height' => 200 );
698 $this->WP_Widget( 'rss', __('RSS'), $widget_ops, $control_ops );
701 function widget($args, $instance) {
703 if ( isset($instance['error']) && $instance['error'] )
706 extract($args, EXTR_SKIP);
708 $url = $instance['url'];
709 while ( stristr($url, 'http') != $url )
710 $url = substr($url, 1);
715 // self-url destruction sequence
716 if ( $url == site_url() || $url == home_url() )
719 $rss = fetch_feed($url);
720 $title = $instance['title'];
724 if ( ! is_wp_error($rss) ) {
725 $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
727 $title = esc_html(strip_tags($rss->get_title()));
728 $link = esc_url(strip_tags($rss->get_permalink()));
729 while ( stristr($link, 'http') != $link )
730 $link = substr($link, 1);
734 $title = empty($desc) ? __('Unknown Feed') : $desc;
736 $title = apply_filters('widget_title', $title, $instance, $this->id_base);
737 $url = esc_url(strip_tags($url));
738 $icon = includes_url('images/rss.png');
740 $title = "<a class='rsswidget' href='$url' title='" . esc_attr__( 'Syndicate this content' ) ."'><img style='border:0' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
744 echo $before_title . $title . $after_title;
745 wp_widget_rss_output( $rss, $instance );
748 if ( ! is_wp_error($rss) )
753 function update($new_instance, $old_instance) {
754 $testurl = ( isset($new_instance['url']) && ($new_instance['url'] != $old_instance['url']) );
755 return wp_widget_rss_process( $new_instance, $testurl );
758 function form($instance) {
760 if ( empty($instance) )
761 $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
762 $instance['number'] = $this->number;
764 wp_widget_rss_form( $instance );
769 * Display the RSS entries in a list.
773 * @param string|array|object $rss RSS url.
774 * @param array $args Widget arguments.
776 function wp_widget_rss_output( $rss, $args = array() ) {
777 if ( is_string( $rss ) ) {
778 $rss = fetch_feed($rss);
779 } elseif ( is_array($rss) && isset($rss['url']) ) {
781 $rss = fetch_feed($rss['url']);
782 } elseif ( !is_object($rss) ) {
786 if ( is_wp_error($rss) ) {
787 if ( is_admin() || current_user_can('manage_options') )
788 echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
792 $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
793 $args = wp_parse_args( $args, $default_args );
794 extract( $args, EXTR_SKIP );
796 $items = (int) $items;
797 if ( $items < 1 || 20 < $items )
799 $show_summary = (int) $show_summary;
800 $show_author = (int) $show_author;
801 $show_date = (int) $show_date;
803 if ( !$rss->get_item_quantity() ) {
804 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
811 foreach ( $rss->get_items(0, $items) as $item ) {
812 $link = $item->get_link();
813 while ( stristr($link, 'http') != $link )
814 $link = substr($link, 1);
815 $link = esc_url(strip_tags($link));
816 $title = esc_attr(strip_tags($item->get_title()));
818 $title = __('Untitled');
820 $desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
821 $desc = wp_html_excerpt( $desc, 360 );
823 // Append ellipsis. Change existing [...] to […].
824 if ( '[...]' == substr( $desc, -5 ) )
825 $desc = substr( $desc, 0, -5 ) . '[…]';
826 elseif ( '[…]' != substr( $desc, -10 ) )
827 $desc .= ' […]';
829 $desc = esc_html( $desc );
831 if ( $show_summary ) {
832 $summary = "<div class='rssSummary'>$desc</div>";
839 $date = $item->get_date( 'U' );
842 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
847 if ( $show_author ) {
848 $author = $item->get_author();
849 if ( is_object($author) ) {
850 $author = $author->get_name();
851 $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
856 echo "<li>$title{$date}{$summary}{$author}</li>";
858 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
869 * Display RSS widget options form.
871 * The options for what fields are displayed for the RSS form are all booleans
872 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
877 * @param array|string $args Values for input fields.
878 * @param array $inputs Override default display options.
880 function wp_widget_rss_form( $args, $inputs = null ) {
882 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
883 $inputs = wp_parse_args( $inputs, $default_inputs );
885 extract( $inputs, EXTR_SKIP);
887 $number = esc_attr( $number );
888 $title = esc_attr( $title );
889 $url = esc_url( $url );
890 $items = (int) $items;
891 if ( $items < 1 || 20 < $items )
893 $show_summary = (int) $show_summary;
894 $show_author = (int) $show_author;
895 $show_date = (int) $show_date;
897 if ( !empty($error) )
898 echo '<p class="widget-error"><strong>' . sprintf( __('RSS Error: %s'), $error) . '</strong></p>';
900 if ( $inputs['url'] ) :
902 <p><label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?></label>
903 <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" /></p>
904 <?php endif; if ( $inputs['title'] ) : ?>
905 <p><label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?></label>
906 <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" /></p>
907 <?php endif; if ( $inputs['items'] ) : ?>
908 <p><label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?></label>
909 <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
911 for ( $i = 1; $i <= 20; ++$i )
912 echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
915 <?php endif; if ( $inputs['show_summary'] ) : ?>
916 <p><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"'; ?>/>
917 <label for="rss-show-summary-<?php echo $number; ?>"><?php _e('Display item content?'); ?></label></p>
918 <?php endif; if ( $inputs['show_author'] ) : ?>
919 <p><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"'; ?>/>
920 <label for="rss-show-author-<?php echo $number; ?>"><?php _e('Display item author if available?'); ?></label></p>
921 <?php endif; if ( $inputs['show_date'] ) : ?>
922 <p><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"'; ?>/>
923 <label for="rss-show-date-<?php echo $number; ?>"><?php _e('Display item date?'); ?></label></p>
926 foreach ( array_keys($default_inputs) as $input ) :
927 if ( 'hidden' === $inputs[$input] ) :
928 $id = str_replace( '_', '-', $input );
930 <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
937 * Process RSS feed widget data and optionally retrieve feed items.
939 * The feed widget can not have more than 20 items or it will reset back to the
940 * default, which is 10.
942 * The resulting array has the feed title, feed url, feed link (from channel),
943 * feed items, error (if any), and whether to show summary, author, and date.
944 * All respectively in the order of the array elements.
948 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
949 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
952 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
953 $items = (int) $widget_rss['items'];
954 if ( $items < 1 || 20 < $items )
956 $url = esc_url_raw(strip_tags( $widget_rss['url'] ));
957 $title = trim(strip_tags( $widget_rss['title'] ));
958 $show_summary = isset($widget_rss['show_summary']) ? (int) $widget_rss['show_summary'] : 0;
959 $show_author = isset($widget_rss['show_author']) ? (int) $widget_rss['show_author'] :0;
960 $show_date = isset($widget_rss['show_date']) ? (int) $widget_rss['show_date'] : 0;
963 $rss = fetch_feed($url);
966 if ( is_wp_error($rss) ) {
967 $error = $rss->get_error_message();
969 $link = esc_url(strip_tags($rss->get_permalink()));
970 while ( stristr($link, 'http') != $link )
971 $link = substr($link, 1);
978 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
982 * Tag cloud widget class
986 class WP_Widget_Tag_Cloud extends WP_Widget {
988 function WP_Widget_Tag_Cloud() {
989 $widget_ops = array( 'description' => __( "Your most used tags in cloud format") );
990 $this->WP_Widget('tag_cloud', __('Tag Cloud'), $widget_ops);
993 function widget( $args, $instance ) {
995 $current_taxonomy = $this->_get_current_taxonomy($instance);
996 if ( !empty($instance['title']) ) {
997 $title = $instance['title'];
999 if ( 'post_tag' == $current_taxonomy ) {
1000 $title = __('Tags');
1002 $tax = get_taxonomy($current_taxonomy);
1003 $title = $tax->labels->name;
1006 $title = apply_filters('widget_title', $title, $instance, $this->id_base);
1008 echo $before_widget;
1010 echo $before_title . $title . $after_title;
1011 echo '<div class="tagcloud">';
1012 wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
1017 function update( $new_instance, $old_instance ) {
1018 $instance['title'] = strip_tags(stripslashes($new_instance['title']));
1019 $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
1023 function form( $instance ) {
1024 $current_taxonomy = $this->_get_current_taxonomy($instance);
1026 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
1027 <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
1028 <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
1029 <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
1030 <?php foreach ( get_object_taxonomies('post') as $taxonomy ) :
1031 $tax = get_taxonomy($taxonomy);
1032 if ( !$tax->show_tagcloud || empty($tax->labels->name) )
1035 <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
1036 <?php endforeach; ?>
1040 function _get_current_taxonomy($instance) {
1041 if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
1042 return $instance['taxonomy'];
1049 * Navigation Menu widget class
1053 class WP_Nav_Menu_Widget extends WP_Widget {
1055 function WP_Nav_Menu_Widget() {
1056 $widget_ops = array( 'description' => __('Use this widget to add one of your custom menus as a widget.') );
1057 parent::WP_Widget( 'nav_menu', __('Custom Menu'), $widget_ops );
1060 function widget($args, $instance) {
1062 $nav_menu = wp_get_nav_menu_object( $instance['nav_menu'] );
1067 $instance['title'] = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
1069 echo $args['before_widget'];
1071 if ( !empty($instance['title']) )
1072 echo $args['before_title'] . $instance['title'] . $args['after_title'];
1074 wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
1076 echo $args['after_widget'];
1079 function update( $new_instance, $old_instance ) {
1080 $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
1081 $instance['nav_menu'] = (int) $new_instance['nav_menu'];
1085 function form( $instance ) {
1086 $title = isset( $instance['title'] ) ? $instance['title'] : '';
1087 $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
1090 $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
1092 // If no menus exists, direct the user to go and create some.
1094 echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
1099 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
1100 <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
1103 <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
1104 <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
1106 foreach ( $menus as $menu ) {
1107 $selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
1108 echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>';
1118 * Register all of the default WordPress widgets on startup.
1120 * Calls 'widgets_init' action after all of the WordPress widgets have been
1125 function wp_widgets_init() {
1126 if ( !is_blog_installed() )
1129 register_widget('WP_Widget_Pages');
1131 register_widget('WP_Widget_Calendar');
1133 register_widget('WP_Widget_Archives');
1135 register_widget('WP_Widget_Links');
1137 register_widget('WP_Widget_Meta');
1139 register_widget('WP_Widget_Search');
1141 register_widget('WP_Widget_Text');
1143 register_widget('WP_Widget_Categories');
1145 register_widget('WP_Widget_Recent_Posts');
1147 register_widget('WP_Widget_Recent_Comments');
1149 register_widget('WP_Widget_RSS');
1151 register_widget('WP_Widget_Tag_Cloud');
1153 register_widget('WP_Nav_Menu_Widget');
1155 do_action('widgets_init');
1158 add_action('init', 'wp_widgets_init', 1);