You are here:  » Tag Cloud Widget


Tag Cloud Widget

Submitted by support on Thu, 2013-05-02 10:40 in

Hi everyone,

Here is the code for a simple Tag Cloud widget, which will display a random selection of merchants, categories or brands:

class pto_tagcloud_widget extends WP_Widget {
  function pto_tagcloud_widget()
  {
    parent::WP_Widget(false, $name = "PriceTapestry.org Tag Cloud");
  }
  function widget($args, $instance)
  {
    global $pto_config_merchantBaseHREF;
    global $pto_config_categoryBaseHREF;
    global $pto_config_brandBaseHREF;
    global $pto_config_databaseTablePrefix;
    global $wpdb;
    extract($args);
    $title = apply_filters("widget_title", $instance["title"]);
    print $before_widget;
    if ($title) print $before_title.$title.$after_title;
    $module = $instance["module"];
    // customise sizes, colors and number of items here
    $font_size = array("14px","20px","24px");
    $color = array("red","green","blue");
    $items = 10;
    // end customisations
    $sql = "SELECT DISTINCT(".$module.") FROM `".$pto_config_databaseTablePrefix."products` WHERE ".$module." <> '' ORDER BY RAND() LIMIT ".$items;
    if ($numRows = $wpdb->query($sql))
    {
      print "<div id='pto_tagcloud'>";
      foreach($wpdb->last_result as $row)
      {
        $baseHREFVar = "pto_config_".$module."BaseHREF";
        $itemURL = get_bloginfo('url').$$baseHREFVar."/".pto_common_hyphenate($row->$module);
        $style= "text-decoration:none; font-size:".$font_size[array_rand($font_size)]."; color:".$color[array_rand($color)].";";
        print "<a href='".$itemURL."' style='".$style."'>".$row->$module."</a> ";
      }
      print "</div>";
    }
    print $after_widget;
  }
  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance["title"] = strip_tags($new_instance["title"]);
    $instance["module"] = strip_tags($new_instance["module"]);
    return $instance;
  }
  function form($instance)
  {
    $title = esc_attr($instance["title"]);
    print "<p><label for='".$this->get_field_id('title')."'>"._e("Title:")." <input class='widefat' id='".$this->get_field_id('title')."' name='".$this->get_field_name('title')."' type='text' value='".htmlentities($title,ENT_QUOTES,get_settings("blog_charset"))."' /></label></p>";
    $module = esc_attr($instance["module"]);
    print "<p><label for='".$this->get_field_id('module')."'>"._e("Module:")." <select id='".$this->get_field_id('module')."' name='".$this->get_field_name('module')."'><option ".($module=="merchant"?"selected='selected'":"")." value='merchant'>Merchant</option><option ".($module=="category"?"selected='selected'":"")." value='category'>Category</option><option ".($module=="brand"?"selected='selected'":"")." value='brand'>Brand</option></select></label></p>";
  }
}

To use, simply paste the code into the end of your pto.php file (just before the closing PHP tag), and then look for the following code at line 115:

  register_widget('pto_atoz_widget');

...and REPLACE with:

  register_widget('pto_atoz_widget');
  register_widget('pto_tagcloud_widget');

The module (merchant, category or brand) can be selected on a per-widget basis on the Widgets configuration page after dragging "PriceTapestry.org Tag Cloud" to the required widget area, where you can also give that instance an optional title.

To configure the number of items selected, and your choice of random colours and front sizes simply modify this section of the above code as required:

    // customise sizes, colors and number of items here
    $font_size = array("14px","20px","24px");
    $color = array("red","green","blue");
    $items = 10;
    // end customisations

Cheers!
David
--
PriceTapestry.com