3 * Widget API: WP_Widget_Tag_Cloud class
11 * Core class used to implement a Tag cloud widget.
17 class WP_Widget_Tag_Cloud extends WP_Widget {
20 * Sets up a new Tag Cloud widget instance.
25 public function __construct() {
26 $widget_ops = array( 'description' => __( "A cloud of your most used tags.") );
27 parent::__construct('tag_cloud', __('Tag Cloud'), $widget_ops);
31 * Outputs the content for the current Tag Cloud widget instance.
36 * @param array $args Display arguments including 'before_title', 'after_title',
37 * 'before_widget', and 'after_widget'.
38 * @param array $instance Settings for the current Tag Cloud widget instance.
40 public function widget( $args, $instance ) {
41 $current_taxonomy = $this->_get_current_taxonomy($instance);
42 if ( !empty($instance['title']) ) {
43 $title = $instance['title'];
45 if ( 'post_tag' == $current_taxonomy ) {
48 $tax = get_taxonomy($current_taxonomy);
49 $title = $tax->labels->name;
54 * Filter the taxonomy used in the Tag Cloud widget.
57 * @since 3.0.0 Added taxonomy drop-down.
61 * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'.
63 $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
64 'taxonomy' => $current_taxonomy,
68 if ( empty( $tag_cloud ) ) {
72 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
73 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
75 echo $args['before_widget'];
77 echo $args['before_title'] . $title . $args['after_title'];
80 echo '<div class="tagcloud">';
85 echo $args['after_widget'];
89 * Handles updating settings for the current Tag Cloud widget instance.
94 * @param array $new_instance New settings for this instance as input by the user via
96 * @param array $old_instance Old settings for this instance.
97 * @return array Settings to save or bool false to cancel saving.
99 public function update( $new_instance, $old_instance ) {
101 $instance['title'] = sanitize_text_field( stripslashes( $new_instance['title'] ) );
102 $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
107 * Outputs the Tag Cloud widget settings form.
112 * @param array $instance Current settings.
114 public function form( $instance ) {
115 $current_taxonomy = $this->_get_current_taxonomy($instance);
116 $title_id = $this->get_field_id( 'title' );
117 $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
119 echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label>
120 <input type="text" class="widefat" id="' . $title_id .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" />
123 $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
124 $id = $this->get_field_id( 'taxonomy' );
125 $name = $this->get_field_name( 'taxonomy' );
126 $input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
128 switch ( count( $taxonomies ) ) {
130 // No tag cloud supporting taxonomies found, display error message
132 echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
133 printf( $input, '' );
136 // Just a single tag cloud supporting taxonomy found, no need to display options
138 $keys = array_keys( $taxonomies );
139 $taxonomy = reset( $keys );
140 printf( $input, esc_attr( $taxonomy ) );
143 // More than one tag cloud supporting taxonomy found, display options
146 '<p><label for="%1$s">%2$s</label>' .
147 '<select class="widefat" id="%1$s" name="%3$s">',
153 foreach ( $taxonomies as $taxonomy => $tax ) {
155 '<option value="%s"%s>%s</option>',
156 esc_attr( $taxonomy ),
157 selected( $taxonomy, $current_taxonomy, false ),
162 echo '</select></p>';
167 * Retrieves the taxonomy for the current Tag cloud widget instance.
172 * @param array $instance Current settings.
173 * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
175 public function _get_current_taxonomy($instance) {
176 if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
177 return $instance['taxonomy'];