How to create wordpress widget plugin, In this article, given full code and with explanation. For creating wordpress widget you just need use our code. In many wordpress themes we want to create the wordpress widget. For creating the wordpress widget you just need to put following code in functions.php file.
How to create wordpress widget plugin
This functions.php file you can find in wordpress theme folder.
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
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->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']); ?></pre> 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; ?>" /> <pre> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->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> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php foreach ($popularPosts as $post) { ?> <li> php echo $post['url'];?>"><!--?php echo $post['title']; ?--> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php if ($displayComments) { ?> (<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php echo $post['comment_count'] . ' ' . __('comments', BM_THEMENAME); ?>) <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php } ?> </li> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php } ?> </ol> <?php echo $args['after_widget']; } } ?>