ILWP Colored Tag Cloud plugin, please visit the plugin page. Feel free to leave comments or post feature requests. * Version: 2.2a * Author: Steve Johnson * Author URI: http://ilikewp.com/ */ /* Copyright 2009-2012 Steve Johnson (email : steve@ilikewp.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ define( 'ILWP_CTC_VERSION', '2.2' ); define( 'CTC_DEBUG', false ); function ilwp_tag_cloud( $args = '' ) { // for those calling the function directly from template, // add default arguments $default_colors = array( 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'white', 'yellow'); $default['min_size'] = 8; $default['max_size'] = 40; $default['number'] = 0; $default['use_colors'] = 1; $default['sort'] = 'random'; $default['order'] = 'ASC'; $default['color_names'] = $default_colors; $args = wp_parse_args( $args, $default ); ## if they leave the colors field blank if ( '' == $args['color_names'][0] ) $args['color_names'] = $default_colors; // Always query top tags $tags = get_tags( array('orderby' => 'count', 'order' => 'DESC' ) ); if ( empty( $tags ) || 2 > sizeof( $tags ) || !is_array( $tags ) ) return; $return = ilwp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args if ( is_wp_error( $return ) ) return false; echo $return; } function ilwp_generate_tag_cloud( $tags, $args = '' ) { global $wp_rewrite; extract( $args ); // $min_size, $max_size, $number, $color_names, $use_colors, $sort, $order if ( 'random' == $sort ) { shuffle($tags); } else { // SQL cannot save you; this is a second (potentially different) sort on a subset of data. if ( 'name' == $sort ) uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') ); else uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') ); if ( 'DESC' == $order ) $tags = array_reverse( $tags, true ); } if ( $number > 0 ) $tags = array_slice( $tags, 0, $number ); $counts = $tag_links = array(); foreach ( $tags as $tag ) { $counts[ $tag->name ] = $tag->count; $tag_links[ $tag->name ] = get_tag_link( $tag->term_id ); $tag_ids[ $tag->name ] = $tag->term_id; } $min_count = min($counts); $spread = max($counts) - $min_count; if ( $spread <= 0 ) $spread = 1; $font_spread = $max_size - $min_size; if ( $font_spread <= 0 ) $font_spread = 1; $font_step = $font_spread / $spread; $a = array(); $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : ''; $c = sizeof( $color_names ); foreach ( $counts as $tag => $count ) { $tag_id = $tag_ids[$tag]; $tag_link = clean_url($tag_links[$tag]); if ( $use_colors ) : $color = rand( 0, $c - 1 ); $colorstyle = " color: " . $color_names[$color] . ";"; else : $colorstyle = ""; endif; $a[] = "$tag"; } $cloud = join("\n", $a); return $cloud; } /** * Version 2.0 * Switches to multi-instance widget via WP_Widget API */ // going to change the options for this version to easily // check for out-of-date plugins, as I neglected to add-in // a version option if ( !get_option('widget_ilwp_tag_cloud_version') ) update_option( 'widget_ilwp_tag_cloud_version', '2.0' ); if ( get_option( 'ilwp_widget_tag_cloud' ) ) delete_option( 'ilwp_widget_tag_cloud' ); /** * ILWPColoredTagCloud Class */ class ILWPColoredTagCloud extends WP_Widget { function ILWPColoredTagCloud() { $widget_ops = array('classname' => 'ilwp_widget_tag_cloud', 'description' => __( "A really cool COLORED tag cloud") ); $control_ops = array('width' => 400, 'height' => 350); $this->WP_Widget( 'ilwp_tag_cloud', __('ILWP Colored Tag Cloud'), $widget_ops, $control_ops ); } /** @see WP_Widget::widget */ function widget( $args, $instance ) { extract( $args ); $title = ( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : __('Tags') ; echo $before_widget; echo $before_title . $title . $after_title; ilwp_tag_cloud( $instance ); echo $after_widget . "\n\n"; } /** @see WP_Widget::update */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = ( 0 == $new_instance['number'] ) ? 0 : intval( $new_instance['number'] ); $instance['min_size'] = ( $new_instance['min_size'] > 10 ) ? 10 : intval( $new_instance['min_size'] ); $instance['max_size'] = ( $new_instance['max_size'] > 40 && $instance['min_size'] < $new_instance['max_size'] ) ? 40 : intval( $new_instance['max_size'] ); if ( $instance['max_size'] < $instance['min_size'] + 2 ) $instance['max_size'] = intval( $instance['min_size'] + 2 ); $instance['use_colors'] = $new_instance['use_colors']; $instance['sort'] = $new_instance['sort']; $instance['order'] = $new_instance['order']; ## get color names/numbers into an array $str = $new_instance['color_names']; ## replace commas with spaces $str = str_replace( ',', ' ', $str ); ## replace spaces with pipes $str = preg_replace('/\s+/', '|', $str); $str = trim( $str, "|" ); $newcolors = explode( '|', $str ); $instance['color_names'] = $newcolors; return $instance; } /** @see WP_Widget::form */ function form( $instance ) { $default_colors = array( 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'white', 'yellow'); $default['color_names'] = $default_colors; $default['min_size'] = 8; $default['max_size'] = 40; $default['use_colors'] = 1; $default['number'] = 0; $default['sort'] = 'random'; $default['order'] = 'ASC'; if ( CTC_DEBUG ) print_r( $default ); $instance = wp_parse_args( $instance, $default ); if ( CTC_DEBUG ) print_r( $instance ); extract( $instance ); $title = esc_attr( $instance['title']); $colors = implode( $instance['color_names'], "\r\n" ); ?>
Sort direction?