Trying to Write My Own Widget
Dec 24th, 2006 by Brian A. Thomas
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
-
/*
-
Plugin Name: UTW Tag cloud widget
-
Description: A simplified tag cloud widget - requires <a href="http://www.neato.co.nz/ultimate-tag-warrior/">Ultimate Tag Warrior</a>...
-
Author: Brian A. Thomas
-
Version: 1.0
-
Author URI: http://www.brianathomas.com
-
*/
-
-
/*
-
For this plugin, I basically copied the gsearch.php plugin and modified it to my needs
-
Eventually I'll expand this to actually use the title option...
-
*/
-
-
function widget_simple_utw_cloud_init() {
-
-
// Check for the required plugin functions. This will prevent fatal
-
// errors occurring when you deactivate the dynamic-sidebar plugin.
-
return;
-
-
function simple_utw_cloud($args){
-
-
// $args is an array of strings that help widgets to conform to
-
// the active theme: before_widget, before_title, after_widget,
-
// and after_title are the array keys. Default tags: li and h2.
-
-
// Each widget can store its own options. We keep strings here.
-
$options = get_option('widget_simple_utw_cloud');
-
$title = $options['title'];
-
//$order = $options['order'];
-
-
// These generate the output
-
echo '<h3>Tag Cloud</h3>';
-
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>';
-
//if $order = '0' {
-
// UTW_ShowWeightedTagSet("sizedtagcloud");
-
//}
-
//else {
-
UTW_ShowWeightedTagSetAlphabetical("coloredsizedtagcloud","",0);
-
//}
-
echo $after_widget;
-
}
-
-
function simple_utw_cloud_control(){
-
$options = get_option('widget_simple_utw_cloud');
-
if ( $_POST['simple_utw_cloud-submit'] ) {
-
-
// Remember to sanitize and format use input appropriately.
-
$options['order'] = '1';
-
update_option('widget_simple_utw_cloud', $options);
-
}
-
-
// Be sure you format your options to be valid HTML attributes.
-
-
// This displays the Options form
-
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>';
-
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>';
-
echo '<input type="hidden" id="simple_utw_cloud-submit" name="simple_utw_cloud-submit" value="1" />';
-
-
}
-
-
// This registers our widget so it appears with the other available
-
// widgets and can be dragged and dropped into any active sidebars.
-
register_sidebar_widget('Simple UTW Cloud', 'widget_simple_utw_cloud');
-
-
// This registers our optional widget control form. Because of this
-
// our widget will have a button that reveals a 300x100 pixel form.
-
register_widget_control('Simple UTW Cloud', 'widget_simple_utw_cloud_control');
-
-
}
-
-
// Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
-
add_action('plugins_loaded', 'widget_simple_utw_cloud_init');
-
-
?>
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.






