A sidebar widget for stickymenu

September 26th, 2007 tela

Almost one month ago there was this comment posted on Tela-Web that totally stayed unnoticed as it went in the moderation queue. This was the code for a Sticky Menu sidebar widget.

We haven’t had any chance to test it but we thought that it might interest some people out there. As it seems that this code is not published on the author website we decided to put it here.

We can not assure any support for this. We didn’t test it. And as we haven’t programmed any widget yet we have absolutely no clue wether this code is good or not and how to implement it.

So if you feel like testing it, this is entirely to your own risks.

Here is the comment as it was posted:

——————————————————————————–

Wordpress has gone in the direction of widgets. A sidebar widget for stickymenu seems like a good idea, so here it is.

<?php /* 
Plugin Name: Menu Widget 
Plugin URI: http://guff.szub.net/my-widget-example-wordpress-widget 
Description:Sidebar widget to place sticky menus. Adapted from  Kat Oseo's example widget
Author: Kaf Oseo, Jim Hinds 
Version: 0.07
Author URI: http://jahbini.org/ 

    Might Menu Widget is released under the GNU General Public License (GPL) 
    http://www.gnu.org/licenses/gpl.txt 

    This is a WordPress plugin (http://wordpress.org) and widget 
    (http://automattic.com/code/widgets/). 
*/ 

// We're putting the plugin's functions in one big function we then 
// call at 'plugins_loaded' (add_action() at bottom) to ensure the 
// required Sidebar Widget functions are available. 
function widget_mightymenu_init() { 

    // Check to see required Widget API functions are defined... 
    if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') ) 
        return; // ...and if not, exit gracefully from the script. 
	$current_plugins = get_option('active_plugins'); 
        if (!in_array('stickymenu/stickymenu.php', $current_plugins)) {
		?> <em> Menu Widget Not activated as it requires Sticky Menu </em> <?php ;
			return;
		}
 
    // This function prints the sidebar widget--the cool stuff! 
    function widget_mightymenu($args,$number = 1) { 

        // $args is an array of strings which help your widget 
        // conform to the active theme: before_widget, before_title, 
        // after_widget, and after_title are the array keys. 
        extract($args); 

        // Collect our widget's options, or define their defaults. 
        $options = get_option('widget_mightymenu'); 
        $title = empty($options[$number]['title']) ? 'Might Menu Widget' : $options[$number]['title']; 
        $text = empty($options[$number]['text']) ? 'Hello World!' : $options[$number]['text']; 

         // It's important to use the $before_widget, $before_title, 
         // $after_title and $after_widget variables in your output. 
        echo $before_widget; 
        echo $before_title . $title . $after_title; 

        $menu = new stickymenu; $menu -> display_menu("menu=$text"); 
        echo $after_widget; 
    } 

    // This is the function that outputs the form to let users edit 
    // the widget's title and so on. It's an optional feature, but 
    // we'll use it because we can! 
    function widget_mightymenu_control($number) { 

        // Collect our widget's options. 
        $options = $newoptions = get_option('widget_mightymenu'); 
		if (!is_array($options)) $options = $newoptions = array();
        // This is for handing the control form submission. 
        if ( $_POST["mightymenu-submit-$number"] ) { 
            // Clean up control form submission options 
            $newoptions[$number]['title'] = stripslashes($_POST["mightymenuwidget-title-$number"]); 
            $newoptions[$number]['text'] = strip_tags(stripslashes($_POST["mightymenuwidget-text-$number"])); 
        } 

        // If original widget options do not match control form 
        // submission options, update them. 
        if ( $options != $newoptions ) { 
            $options = $newoptions; 
            update_option('widget_mightymenu', $options); 
        } 

        // Format options as valid HTML. Hey, why not. 
        $title = htmlspecialchars($options[$number]['title'], ENT_QUOTES); 
        $text = htmlspecialchars($options[$number]['text'], ENT_QUOTES); 

// The HTML below is the control form for editing options. 
global $wpdb;
$table_name = $wpdb->prefix . 'stickymenu';
# Retrieve menu list            
$sql = "SELECT DISTINCT menu FROM `$table_name`"; 
$menu_list_array = $wpdb->get_results($sql, ARRAY_A);
//echo "<div id='JAHBINI'>";
//print_a($menu_list_array);
//echo "</div>";
?> 
        <div> 
        <label for="mightymenuwidget-title" style="line-height:35px;display:block;">Menu Heading: <input type="text" id="mightymenuwidget-title-<?php echo $number?>" name="mightymenuwidget-title-<?php echo $number?>" value="<?php echo $title; ?>" /></label> 
        <label for="mightymenuwidget-text" style="line-height:35px;display:block;">Menu Name: 
			<select name="mightymenuwidget-text-<?php echo $number?>" style="width:150px;">
			<?php 

			$menu_count = 0;        
			if ($menu_list_array) { 
			  foreach($menu_list_array as $this_menu) {	?>
			<option value="<?php echo $this_menu['menu']; ?>" <?php echo (($this_menu['menu'] == $text)? 'selected' : ''); ?>><?php echo $this_menu['menu']; ?></option>
			<?php };
		};
			?>
			</select>
	
	</label> 
        <input type="hidden" name="mightymenu-submit-<?php echo $number?>" id="mightymenu-submit-<?php echo $number?>" value="1" /> 
        </div> 
    <?php 
    // end of widget_mightymenu_control() 
    } 

	function widget_mightymenu_register() {
	        $options = get_option('widget_mightymenu');
	        $number = $options['number'];
	        if ( $number < 1 ) $number = 1;
	        if ( $number > 9 ) $number = 9;
	        $dims = array('width' => 460, 'height' => 350);
	        $class = array('classname' => 'widget_mightymenu');
	        for ($i = 1; $i <= 9; $i++) {
	                $name = sprintf(__('MMenu %d'), $i);
	                $id = "mightymenu-$i"; // Never never never translate an id
	                wp_register_sidebar_widget($id, $name, $i <= $number ? 'widget_mightymenu' : /* unregister */ '', $class, $i);
	                wp_register_widget_control($id, $name, $i <= $number ? 'widget_mightymenu_control' : /* unregister */ '', $dims, $i);
	        }                                                                                                                                             

	        add_action('sidebar_admin_setup', 'widget_mightymenu_setup');
	        add_action('sidebar_admin_page', 'widget_mightymenu_page');
	}
	function widget_mightymenu_setup() {
	        $options = $newoptions = get_option('widget_mightymenu');                                                                                            
	        if ( isset($_POST['mightymenu-number-submit']) ) {                                                                                                   
	                $number = (int) $_POST['mightymenu-number'];                                                                                                 
	                if ( $number > 9 ) $number = 9;                                                                                                        
	                if ( $number < 1 ) $number = 1;                                                                                                        
	                $newoptions['number'] = $number;                                                                                                       
	        }                                                                                                                                              
	        if ( $options != $newoptions ) {                                                                                                               
	                $options = $newoptions;                                                                                                                
	                update_option('widget_mightymenu', $options);                                                                                                
	                widget_mightymenu_register(); 
	echo "options updated to NUMBER = $number";                                                                                          
	        }                                                                                                                                              
	}
	function widget_mightymenu_page() {                                                                                                                       
	        $options = $newoptions = get_option('widget_mightymenu');                                                                                            
	?>                                                                                                                                                     
	        <div class="wrap">                                                                                                                             
	                <form method="POST">                                                                                                                   
	                        <h2><?php _e('Menu Widgets'); ?></h2>                                                                                          
	                        <p style="line-height: 30px;"><?php _e('How many Mighty Menu widgets would you like?'); ?>                                            
	                        <select id="mightymenu-number" name="mightymenu-number" value="<?php echo $options['number']; ?>">                                         
	<?php for ( $i = 1; $i < 10; ++$i ) {
		echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; 
		} ?>                
	                        </select>                                                                                                                      
	                        <span class="submit"><input type="submit" name="mightymenu-number-submit" id="mightymenu-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p>
	                </form>                                                                                                                                
	        </div>                                                                                                                                         
	<?php                                                                                                                                                  
	}                                                                                                                                                      
	
    // This registers the widget. About time. 
	widget_mightymenu_register();
} 

// Delays plugin execution until Dynamic Sidebar has loaded first. 
add_action('plugins_loaded', 'widget_mightymenu_init'); 
?>

Entry Filed under: WordPress plugins

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed