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() {
27 'description' => __( 'A cloud of your most used tags.' ),
28 'customize_selective_refresh' => true,
30 parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
34 * Outputs the content for the current Tag Cloud widget instance.
39 * @param array $args Display arguments including 'before_title', 'after_title',
40 * 'before_widget', and 'after_widget'.
41 * @param array $instance Settings for the current Tag Cloud widget instance.
43 public function widget( $args, $instance ) {
44 $current_taxonomy = $this->_get_current_taxonomy($instance);
45 if ( !empty($instance['title']) ) {
46 $title = $instance['title'];
48 if ( 'post_tag' == $current_taxonomy ) {
51 $tax = get_taxonomy($current_taxonomy);
52 $title = $tax->labels->name;
57 * Filters the taxonomy used in the Tag Cloud widget.
60 * @since 3.0.0 Added taxonomy drop-down.
64 * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'.
66 $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
67 'taxonomy' => $current_taxonomy,
71 if ( empty( $tag_cloud ) ) {
75 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
76 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
78 echo $args['before_widget'];
80 echo $args['before_title'] . $title . $args['after_title'];
83 echo '<div class="tagcloud">';
88 echo $args['after_widget'];
92 * Handles updating settings for the current Tag Cloud widget instance.
97 * @param array $new_instance New settings for this instance as input by the user via
99 * @param array $old_instance Old settings for this instance.
100 * @return array Settings to save or bool false to cancel saving.
102 public function update( $new_instance, $old_instance ) {
104 $instance['title'] = sanitize_text_field( $new_instance['title'] );
105 $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
110 * Outputs the Tag Cloud widget settings form.
115 * @param array $instance Current settings.
117 public function form( $instance ) {
118 $current_taxonomy = $this->_get_current_taxonomy($instance);
119 $title_id = $this->get_field_id( 'title' );
120 $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
122 echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label>
123 <input type="text" class="widefat" id="' . $title_id .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" />
126 $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
127 $id = $this->get_field_id( 'taxonomy' );
128 $name = $this->get_field_name( 'taxonomy' );
129 $input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
131 switch ( count( $taxonomies ) ) {
133 // No tag cloud supporting taxonomies found, display error message
135 echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
136 printf( $input, '' );
139 // Just a single tag cloud supporting taxonomy found, no need to display options
141 $keys = array_keys( $taxonomies );
142 $taxonomy = reset( $keys );
143 printf( $input, esc_attr( $taxonomy ) );
146 // More than one tag cloud supporting taxonomy found, display options
149 '<p><label for="%1$s">%2$s</label>' .
150 '<select class="widefat" id="%1$s" name="%3$s">',
156 foreach ( $taxonomies as $taxonomy => $tax ) {
158 '<option value="%s"%s>%s</option>',
159 esc_attr( $taxonomy ),
160 selected( $taxonomy, $current_taxonomy, false ),
165 echo '</select></p>';
170 * Retrieves the taxonomy for the current Tag cloud widget instance.
175 * @param array $instance Current settings.
176 * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
178 public function _get_current_taxonomy($instance) {
179 if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
180 return $instance['taxonomy'];