• Hi everyone.
    I am Dhruva Sagar. My blog is https://www.dhruvasagar.com/blog/

    I have written a rather small widget plugin named Hit Counter. I wasn’t able to find a good one on my own, and wanted to learn plugin creation as well so I went ahead in trying to create one on my own.

    I just have a very simple widget that echos some html code that I get from free hit counter sites, and hence displays the counter. Everything is working fine, until I tried to enhance my plugin so that I could specify the options on the widget page in presentation, that is it’s options.

    Unfortunately, I am facing some rather weird problems in doing that. There is no error, but upon activating my plugin, the widget section under the presentation goes weird, it doesn’t do anything while I click on the options for any widget. Not just for the widget I created, but for none of them.

    The code is :

    <?php
    /*
    Plugin Name: Hit Counter Widget
    Plugin URI: https://www.dhruvasagar.com/
    Description: Adds a counter widget to display the hit counter.
    Author: Dhruva Sagar
    Version: 1.0
    Author URI: https://www.dhruvasagar.com/
    */
    
    // This gets called at the init action
    function widget_counter_init()
    {
    	// Check for the required API functions
    	if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
    		return;
    
    	// This saves options and prints the widget's config form.
    	function widget_counter_control()
    	{
    		$options = $newoptions = get_option('widget_counter');
    		if ( $_POST('counter-submit') )
    		{
    			$newoptions['title'] = $_POST['counter-title'];
    			$newoptions['before'] = $_POST['counter-before'];
    			$newoptions['code'] = $_POST['counter-code'];
    			$newoptions['after'] = $_POST['counter-after'];
    		}
    		if( $options != $newoptions )
    		{
    			$options = $newoptions;
    			update_option('widget_counter', $options);
    		}
    	?>
    
    		<div style="text-align:right">
    			<label for="counter-title" style="line-height:35px;display:block;">Widget Title: <input type="text" id="counter-title" name="counter-title" value="<?php echo $options['title']; ?>" /></label>
    			<label for="counter-before" style="line-height:35px;display:block;">Text before counter: <input type="text" id="counter-before" name="counter-before" value="<?php echo $options['before']; ?>" /></label>
    			<label for="counter-code" style="line-height:35px;display:block;">Counter Code: <textarea id="counter-code" name="counter-code" style="width:290px;height:20px;"><?php echo $options['code']; ?></textarea></label>
    			<label for="counter-after" style="line-height:35px;display:block;">Text after counter: <input type="text" id="counter-after" name="counter-after" value="<?php echo $options['after']; ?>" /></label>
    			<input type="hidden" name="counter-submit" id="counter-submit" value="1" />
    		</div>
    
    	<?php
    	}
    
    	// This prints the widget
    	function widget_counter($args)
    	{
    		extract($args);
    		$defaults = array('title' => 'Hit Counter', 'before' => '', 'code' => '<center><img border="0" alt="hit counter" src="https://xyz.freeweblogger.com/counter/index.php?u=1192707884&s=a" ALIGN="middle" HSPACE="4" VSPACE="2"><script src=https://xyz.freeweblogger.com/counter/script.php?u=1192707884></script></center>', 'after' => '');
    		$options = (array) get_option('widget_counter');
    
    		foreach ( $defaults as $key => $value )
    			if ( !isset($options[$key]) )
    				$options[$key] = $defaults[$key];
    
    		echo $before_widget;
    		echo $before_title . $options['title'] . $after_title;
    		echo '<p>';
    		echo $options['before'] . ' ' . $options['code'] . ' ' . $options['after'];
    		echo '</p>';
    		echo $after_widget;
    	}
    
    	// Tell Dynamic Sidebar about our new widget and its control
    	register_sidebar_widget('Hit Counter', 'widget_counter');
    	register_widget_control('Hit Counter', 'widget_counter_control', 315, 420);
    }
    
    // Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
    add_action('init', 'widget_counter_init');
    
    ?>

    Hope anybody can help me out, once I get going with this plugin, i’ll really start writing my own plugins very regularly.

    Thanks & Regards
    Dhruva Sagar

  • The topic ‘Help needed regarding a widget plugin I am working on’ is closed to new replies.