New WP_Widget Class In WordPress 2.8

In WordPress 2.8, there is a new WP_Widget class, which personally I like it very much as now the multi-instances of widgets is handled by WordPress, all you need to do is just to extends the WP_Widget class and overwrite 3 of the functions namely widgets(), update() and form();

Below is a sample code taken from my WP-Polls that displays Polls Widget. It is tested and it works perfectly. Hope it is useful for plugin authors.

 __('Put a poll that you have added in WP-Polls on your sidebar', 'wp-polls'));
		$this->WP_Widget('polls', __('Polls'), $widget_ops);
	}

	// Display Widget
	function widget($args, $instance) {
		extract($args);
		$title = esc_attr($instance['title']);
		$poll_id = intval($instance['poll_id']);
		$display_pollarchive = intval($instance['display_pollarchive']);
		echo $before_widget.$before_title.$title.$after_title;
		get_poll($poll_id);
		if($display_pollarchive) {
			display_polls_archive_link();
		}
		echo $after_widget;
	}

	// When Widget Control Form Is Posted
	function update($new_instance, $old_instance) {
		if (!isset($new_instance['submit'])) {
			return false;
		}
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['poll_id'] = intval($new_instance['poll_id']);
		$instance['display_pollarchive'] = intval($new_instance['display_pollarchive']);
		return $instance;
	}

	// DIsplay Widget Control Form
	function form($instance) {
		global $wpdb;
		$instance = wp_parse_args((array) $instance, array('title' => __('Polls', 'wp-polls'), 'poll_id' => 0, 'display_pollarchive' => 1));
		$title = esc_attr($instance['title']);
		$poll_id = intval($instance['poll_id']);
		$display_pollarchive = intval($instance['display_pollarchive']);
?>


			
		


			
		


			
		


1 Star2 Stars3 Stars4 Stars5 Stars (172 votes, average: 4.01 out of 5)

Dashboard Widget (Without Controls) Sample Plugin

WordPress 2.5 has the ability to customize your administration dashboard with the help of widgets.

After poking around WordPress 2.5 codes, I have come out with this Dashboard Widget Sample Plugin. BUT do note that this plugin does not include Dashboard Widget Controls. If I have the time, I will add that in or perhaps I will create another plugin called “Dashboard Widget (With Controls) Sample Plugin”.

To get a clearer picture of what this sample plugin does, here is a screenshot of the Dashboard Widget (Without Controls) Sample in action.

Here comes the code:

 'Full URL For "See All" link', // Example: 'index.php?page=wp-useronline/wp-useronline.php'
		'feed_link' => 'Full URL For "RSS" link', // Example: 'index.php?page=wp-useronline/wp-useronline-rss.php'
		'width' => 'half', // OR 'fourth', 'third', 'half', 'full' (Default: 'half')
		'height' => 'single', // OR 'single', 'double' (Default: 'single')
		)
	);
}

### Function: Add Dashboard Widget
add_filter('wp_dashboard_widgets', 'PLUGINNAME_add_dashboard_widget');
function PLUGINNAME_add_dashboard_widget($widgets) {
	global $wp_registered_widgets;
	if (!isset($wp_registered_widgets['dashboard_PLUGINNAME'])) {
		return $widgets;
	}
	array_splice($widgets, sizeof($widgets)-1, 0, 'dashboard_PLUGINNAME');
	return $widgets;
}

### Function: Print Dashboard Widget
function dashboard_PLUGINNAME($sidebar_args) {
	global $wpdb;
	extract($sidebar_args, EXTR_SKIP);
	echo $before_widget;
	echo $before_title;
	echo $widget_name;
	echo $after_title;
	echo 'YOUR CONTENT GOES IN HERE';
	echo $after_widget;
}
?>

Hope this helps some of you guys. =)

1 Star2 Stars3 Stars4 Stars5 Stars (33 votes, average: 3.91 out of 5)