Trying to Write My Own Widget

Calling all WordPress Widget experts, I am trying to write my own widget, but I am not having luck. I get it to show up in the plugins and activate it, but it doesn't show on the widget list.
I know about Chris Johnson's Tag Cloud Widget, but the cloud doesn't show for one thing, and the other is that I don't want it weighted, I want it shown in alphabetical order. It shows the title and nothing more using his... Okay, I got it to display by changing the display from 1 to 50, but that doesn't do a display all like 0 was supposed to do... Okay, I also got it to display in alpa order so I guess the code below is moot. :)
The code appears below the break, with a few comments after the code.

PHP:
  1. <?php
  2. /*
  3. Plugin Name: UTW Tag cloud widget
  4. Description: A simplified tag cloud widget - requires <a href="http://www.neato.co.nz/ultimate-tag-warrior/">Ultimate Tag Warrior</a>...
  5. Author: Brian A. Thomas
  6. Version: 1.0
  7. Author URI: http://www.brianathomas.com
  8. */
  9.  
  10. /*
  11. For this plugin, I basically copied the gsearch.php plugin and modified it to my needs
  12. Eventually I'll expand this to actually use the title option...
  13. */
  14.  
  15. function widget_simple_utw_cloud_init() {
  16.  
  17.     // Check for the required plugin functions. This will prevent fatal
  18.     // errors occurring when you deactivate the dynamic-sidebar plugin.
  19.     if ( !function_exists('register_sidebar_widget') )
  20.         return;
  21.    
  22.     function simple_utw_cloud($args){
  23.    
  24.     // $args is an array of strings that help widgets to conform to
  25.     // the active theme: before_widget, before_title, after_widget,
  26.     // and after_title are the array keys. Default tags: li and h2.
  27.     extract($args);
  28.    
  29.     // Each widget can store its own options. We keep strings here.
  30.     $options = get_option('widget_simple_utw_cloud');
  31.     $title = $options['title'];
  32.     //$order = $options['order'];
  33.    
  34.     // These generate the output   
  35.     echo $before_widget . $before_title . $title . $after_title;
  36.     echo '<h3>Tag Cloud</h3>';
  37.     echo '<p style="text-align:right;"><label for="simple_utw_cloud-title">Title: <input style="width: 200px;" id="gsearch-title" name="simple_utw_cloud-title" type="text" value="'.$title.'" /></label></p>';
  38.     //if $order = '0' {
  39.     //  UTW_ShowWeightedTagSet("sizedtagcloud");
  40.     //}
  41.     //else {
  42.     UTW_ShowWeightedTagSetAlphabetical("coloredsizedtagcloud","",0);
  43.     //}
  44.     echo $after_widget;
  45.     }
  46.    
  47.     function simple_utw_cloud_control(){
  48.     $options = get_option('widget_simple_utw_cloud');
  49.         if ( !is_array($options) )
  50.             $options = array('title'=>'','order'=>'');
  51.         if ( $_POST['simple_utw_cloud-submit'] ) {
  52.  
  53.         // Remember to sanitize and format use input appropriately.
  54.         $options['title'] = strip_tags(stripslashes($_POST['simple_utw_cloud-title']));
  55.         $options['order'] = '1';
  56.         update_option('widget_simple_utw_cloud', $options);
  57.         }
  58.        
  59.         // Be sure you format your options to be valid HTML attributes.
  60.         $title = htmlspecialchars($options['title'], ENT_QUOTES);
  61.        
  62.         // This displays the Options form
  63.         echo '<p style="text-align:right;"><label for="utw_simple_cloud-title">Title: <input style="width: 200px;" id="simple_utw_cloud-title" name="simple_utw_cloud-title" type="text" value="'.$title.'" /></label></p>';
  64.         echo '<p style="text-align:right;"><label for="utw_simple_cloud-order">Order: <input style="width: 200px;" id="utw_simple_cloud-order" name="utw_simple_cloud-order" type="text" value="'.$order.'" /></label></p>';
  65.         echo '<input type="hidden" id="simple_utw_cloud-submit" name="simple_utw_cloud-submit" value="1" />';
  66.  
  67.     }
  68.    
  69.     // This registers our widget so it appears with the other available
  70.     // widgets and can be dragged and dropped into any active sidebars.
  71.     register_sidebar_widget('Simple UTW Cloud', 'widget_simple_utw_cloud');
  72.    
  73.     // This registers our optional widget control form. Because of this
  74.     // our widget will have a button that reveals a 300x100 pixel form.
  75.     register_widget_control('Simple UTW Cloud', 'widget_simple_utw_cloud_control');
  76.    
  77. }
  78.  
  79. // Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
  80. add_action('plugins_loaded', 'widget_simple_utw_cloud_init');
  81.    
  82. ?>

The $options that I have above was supposed to be so that the user sets it to 0 to get the tags to display in weighted form, the 1 would display the cloud in alphabetical order, with the words being bigger if it needed to be heavy or not.
The title may appear twice, I was trying to test something, but since the control never showed up, I wasn't able to test it. I wanted to see if it appeared the correct way which should be an h3 in my case.

About Brian A. Thomas

I am the father of Ari and Sidd. I am the owner and administrator of this site.
PHP, Programming, WordPress , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>