How To Create Custom Widgets


Sponsored Links






WordPress is the most Usable Publishing System I ever use and found, there are so many blogger and internet writer uses WordPress on they blog. WordPress has many free themes and plugins, the other reason is, they add nice featured such as widgets, with WordPress Widgets, we can add anything without touching the themes files, just drag and setting and done ;)

On this WP Tricks, I will show you, how to create Custom Widgets. This tricks written by Darren Hoyt and Ben Gillbanks, and original published on Darren Hyot Blog
Let’s Get Started, first open your functions.php theme and paste or re writes this code

class My_Widget extends WP_Widget {
	function My_Widget() {
		parent::WP_Widget(false, 'Our Test Widget');
	}
function form($instance) {
		// outputs the options form on admin
	}
function update($new_instance, $old_instance) {
		// processes widget options to be saved
		return $new_instance;
	}
function widget($args, $instance) {
		// outputs the content of the widget
	}
}
register_widget('My_Widget');

This code above, only add Example Custom Widget, without some setting and options activated, on the next tricks from Darren Hyot blog, They give example by writing Popular Post widget, let’s do it, below your code above add this code

<?php
class bm_widget_popularPosts extends WP_Widget {

	function bm_widget_popularPosts() {
		parent::WP_Widget(false, 'Popular Posts');
	}

	function widget($args, $instance) {
		$args['title'] = $instance['title'];
		bm_popularPosts($args);
	}

	function update($new_instance, $old_instance) {
		return $new_instance;
	}

	function form($instance) {
		$title = esc_attr($instance['title']);
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<?php
	}
 }
function bm_popularPosts($args = array(), $displayComments = TRUE, $interval = '') {

	global $wpdb;

	$postCount = 5;

	$request = 'SELECT *
		FROM ' . $wpdb->posts . '
		WHERE ';

	if ($interval != '') {
		$request .= 'post_date>DATE_SUB(NOW(), ' . $interval . ') ';
	}

	$request .= 'post_status="publish"
			AND comment_count > 0
		ORDER BY comment_count DESC LIMIT 0, ' . $postCount;

	$posts = $wpdb->get_results($request);

	if (count($posts) >= 1) {

		if (!isset($args['title']) {
			$args['title'] = 'Popular Posts';
		}

		foreach ($posts as $post) {
			wp_cache_add($post->ID, $post, 'posts');
			$popularPosts[] = array(
				'title' => stripslashes($post->post_title),
				'url' => get_permalink($post->ID),
				'comment_count' => $post->comment_count,
			);
		}

		echo $args['before_widget'] . $args['before_title'] . $args['title'] . $args['after_title'];
?>

		<ol>
<?php
		foreach ($popularPosts as $post) {
?>
			<li>
				<a href="<?php echo $post['url'];?>"><?php echo $post['title']; ?></a>
<?php
			if ($displayComments) {
?>
			(<?php echo $post['comment_count'] . ' ' . __('comments', BM_THEMENAME); ?>)
<?php
			}
?>
			</li>
<?php
		}
?>
		</ol>

<?php
		echo $args['after_widget'];
	}
}
?>

And done, you have Popular Post widget and with this similar tricks you can made more sophocated custom widget, such as add Google AdSense widget, Flickr widget, Recent Twitter widgets and Many more

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

How To Create Popular Post without Plugin

Today WP Tricks is how to create popular post without any plugin. We need to run little bit query using some code and place this code on whatever you want to put on. Example we can put this code on … Read More

How to Get Most Commented Posts with Thumbnail

There are so many way to attract visitor on your WordPress blog, the one of them is show most popular post on your sidebar or footer, beside that fact in order to help your visitors finding your best content. Let's … Read More

Create Custom Page with Custom Loop and Query

Created WordPress as CMS? I am guarantee you'll need this tricks. Because WordPress Custom Page is Gregorius tools we can use to create custom design/layout and whatever we want. I will show you how to Create Custom Page with Custom … Read More

Automatically Create a Custom Field

WP Canyon did wonderful tricks, this tricks is about how to automatically create a custom field when a post is published. This wonderful method is added some code on your functions.php files on your current theme. The first time need … Read More

How To Create a Custom WordPress Log-In Screen

WordPress 3.0 is a solid and powerful publishing platform, I like it and really enjoy using it. Using WordPress we can tweak on many area using Plugin or hard coded on our function. Now let's get started made a Custom … Read More

The Best Way to Insert Custom Content into Feeds

My Another Favorite featured in WordPress is easy to tweak it out. And WordPress good support on Feed. Sometime we need to insert custom content into our WordPress Feeds right? On this post I will show you how to Insert … Read More

Qt Designer manual

read more

Source: http://doc.trolltech.com/3.3/designer-manual-7.html

Creating Your Own Widgets using SWT - Eclipse - The Eclipse

On occasion, you will need to create your own custom widgets. There are several reasons that you might want to do this: ยท To add a new type of widget not provided read more

Source: http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm

How to Create Custom Widgets - EzineArticles Submission - Submit

Creating custom widgets can allow you to spruce up your website or web design without totally overhauling it. Scroll bars and button and text boxes, all things that read more

Source: http://ezinearticles.com/?How-to-Create-Custom-Widgets&id=1258042

Creating custom widgets - ZetCode, tutorials for programmers

Creating custom widgets. Have you ever looked at an application and It provides only the very basic widgets and assumes, that the programemer will create the more read more

Source: http://zetcode.com/wxpython/customwidgets/

Custom Widget Libraries

You can add, delete, and organize the widgets in this pane. You can create your custom widgets using the existing widgets (including widgets in other custom widget read more

Source: http://www.axure.com/custom-widget-libraries.aspx


5 Responses to How To Create Custom Widgets

  1. Pingback: designfloat.com

  2. WP GPL says:

    Custom Widget is the best solutions for theme developer to enhanced they theme. And This tricks is really useful.. thanks for share Widgets tricks.

  3. max says:

    thanks for the tips. really useful..

  4. Prashant says:

    thanks it really helped me a lot!!!!!!

  5. erdincgc says:

    Thanks for the nice article. I also wish to beware users that there must be one “)” more at line 46, so, instead of
    if (!isset($args['title'])

    there should be
    if (!isset($args['title']))

    cys

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>