True, there are thousands of widgets available for free on the World Wide Web. Plugins can add a different dimension to your experience with WordPress. However, some might be rather excited at realizing that one can create the customized widgets that would sound tailor made for a specific need. This tutorial teaches you how. Of course, you need a hang of coding to be able to achieve this. Custom widgets can be created by creating a class and registering ne widgets. This is the basic code snippet that you have to paste inside the functions.php file.
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\’);
Now, visit your widgets panel and you will see the following.
We try to create a plugin that displays the headlines of the top 5 posts on the basis of maximum number of comments. We name the widgets class and use the prefix top5_ with the functions and classes. The widget is then named in the constructor. Next, we create last method, a form function to define the editable data, which in this case is the widget\’s title to be shown on the blog. The widget method follows. The $args parameter is what you would want to watch out for here, these being the arguments that are passed on to the register sidebar method, for instance, before_widget and after_widget. Here’s how the final code looks.
<?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 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'];
}
}
?>