Archive | Wordpress

Creating a simple widget in WordPress

Aug 25th, 2011No Comments

We have a standard format in WP for creating a simple WordPress widget .

  1. You need to create a Class Which Extends Wp_Widget .
    class YOURPLUGINNAME extends WP_Widget {
    .....
    ...
    }
    
  2. There are some predefined methods to be used in php .
    1. widget method : The code or content written in it is displayed in front-end , this method looks like ..
      function widget($args, $instance) {
      }
      
  3. The Simple widget Class looks like this .
    
    class YOURPLUGINNAME extends WP_Widget {
    	/** constructor */
    	function YOURPLUGINNAME() {
                     $widget_ops = array (
    			'description' => __('Description of the widget goes here')
    		);
    		parent :: WP_Widget(false, $name = 'Name of the widget', $widget_ops);
    	}
    
    	/** @see WP_Widget::widget */
    	function widget($args, $instance) {
    
    	}
    
    	/** @see WP_Widget::update */
    	function update($new_instance, $old_instance) {
    		$instance = $old_instance;
    		$instance['title'] = strip_tags($new_instance['title']);
    		$instance['email'] = strip_tags($new_instance['email']);
    		$instance['from'] = strip_tags($new_instance['from']);
    		return $instance;
    	}
    
    	/** @see WP_Widget::form */
    	function form($instance) {
    
    	}
    }
    
  4. Finally you need to register the Class for widget creation .
    
    add_action('widgets_init', create_function('', 'return register_widget("YOURPLUGINNAME");'));
    
VN:F [1.9.10_1130]
Rating: 10.0/10 (3 votes cast)
VN:F [1.9.10_1130]
Rating: +4 (from 4 votes)