Tag Cloud - code Rewrite

My Tag cloud implementation started from the Matt’s weighted-categories implementation on hitormiss. It was working fine but I needed two things (yeah, just 2)

  1. the hover text to be more informative
  2. WordPress 2.1 support

I only needed to change one line to make the hover text include the category name (for cases where my font size was tiny). However, all went out the window when I upgraded to WP 2.1. Most of the weighted-categories code had been deprecated over the 2 years since it was first authored. So, I did some digging and figured out the new implementation.

Here is Matt’s code ported to WP 2.1. I still want to replace the regular expression calls with Perl compatible regular expressions for performance reasons.

 

   1:  <?php
   2:   
   3:  function weighted_categories($smallest=10, $largest=48, $unit="pt", $exclude='')
   4:  {
   5:      // the new WP function 'wp_list_categories' outputs the cats for you
   6:      // so it needs to be changed to support 'echo=0'
   7:      $cats = wp_list_categories ('orderby=name&show_count=1&hierarchical=0&echo=0&title_li=0&exclude='
                    . $exclude);
   8:   
   9:       $cats = explode("n", $cats);
  10:      array_pop($cats);
  11:      foreach ($cats as $cat) {
  12:          if (eregi("a href=\"(.+)\" ", $cat, $regs)) {
  13:              $catlink = $regs[1];
  14:              $cat = trim(strip_tags($cat));
  15:              eregi("(.*) (([0-9]+))$", $cat, $regs);
  16:              $catname = $regs[1]; $count = $regs[2];
  17:              $counts{$catname} = $count;
  18:              $catlinks{$catname} = $catlink;
  19:          }
  20:      }
  21:   
  22:      $spread = max($counts) - min($counts); 
  23:      if ($spread <= 0) { $spread = 1; };P
  24:      $fontspread = $largest - $smallest;
  25:      $fontstep = $spread / $fontspread;
  26:      if ($fontspread <= 0) {
  27:          $fontspread = 1;
  28:      }
  29:   
  30:      foreach ($counts as $catname => $count) {
  31:          $catlink = $catlinks{$catname};
  32:          print "<a href=\"$catlink\" title=\"$catname has $count entries\" style=\"font-size: "
                      . ($smallest + ($count/$fontstep)) . "$unit;\">$catname</a> n";
  33:      }
  34:  }
  35:   
  36:  ?>

9 Responses to “Tag Cloud - code Rewrite”

  1. Jürgen Says:

    Hmm, there seems to be something wrong in line 7 (13 on my installation):

    Parse error: syntax error, unexpected ‘=’ in /(myhomedir)/blog/wp-content/plugins/weighted_categories.php on line 13

    I don’t “speak” PHP, however… any ideas?

  2. Glen Says:

    Looks like “sortby” has been removed but was not documented as part of the deprecated code. It should be “orderby”

  3. Jürgen Says:

    No, this doesn’t seem to be the reason, I keep getting the error…

  4. Glen Says:

    have you made the change to the WordPress core code for wp_list_categories(0 to support the “echo” option ? (the function does not have this option but it is needed to use the function as a replacement for the old / deprecated code. - See comments on line 5&6)

  5. Jürgen Says:

    Oh, I understood that comment differently (I thought adding echo=0 to line 7 *was* the patch). What changes to the core code are needed?

  6. jstarek.de - Blogbasteleien » Mal wieder neue Kategorieübersicht Says:

    [...] tut seit dem Update auf Wordpress 2.0 nicht mehr. Der von einem anonymen Blogger vorgeschlagene Patch für das Plugin funktionierte bei mir leider nicht, aber der Autor bringt mir gerade geduldig die [...]

  7. Glen Says:

    The function wp_list_categories (./wp-includes/category-template.php) does not have an option for ‘echo’. If you look at wp_dropdown_categories() (in the same module), you see it has an option for ‘echo’.

    The change I needed in wp_list_categories() was to add the echo option to the $defaults array - add: ‘echo’ => true

    Then, at the end of the function, make the echo optional as follows:

    // allow echo to be turned off
    $output = apply_filters(’wp_list_categories’, $output);
    if ($echo)
    echo $output;
    return $output;

  8. Jürgen Says:

    Thanks for this information; I tried it out and it worked fine!

  9. Glen Says:

    glad to hear it is working for you. I should make a request to WordPress.org to make a provision for wp_list_categories() so it has compatible function to the deprecated code.