How to create wordpress widget plugin

How to create wordpress widget plugin

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.

01class My_Widget extends WP_Widget {
02function My_Widget() {
03parent::WP_Widget(false, 'Our Test Widget');
04}
05function form($instance) {
06// outputs the options form on admin
07}
08function update($new_instance, $old_instance) {
09// processes widget options to be saved
10return $new_instance;
11}
12function widget($args, $instance) {
13// outputs the content of the widget
14}
15}
16register_widget('My_Widget');

this code

01<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
02class bm_widget_popularPosts extends WP_Widget {
03 
04function bm_widget_popularPosts() {
05parent::WP_Widget(false, 'Popular Posts');
06}
07 
08function widget($args, $instance) {
09$args['title'] = $instance['title'];
10bm_popularPosts($args);
11}
12 
13function update($new_instance, $old_instance) {
14return $new_instance;
15}
16 
17function form($instance) {
18$title = esc_attr($instance['title']);
19?></pre>
20php 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; ?>" />
21<pre>
22<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
23}
24}
25function bm_popularPosts($args = array(), $displayComments = TRUE, $interval = '') {
26 
27global $wpdb;
28 
29$postCount = 5;
30 
31$request = 'SELECT *
32FROM ' . $wpdb->posts . '
33WHERE ';
34 
35if ($interval != '') {
36$request .= 'post_date>DATE_SUB(NOW(), ' . $interval . ') ';
37}
38 
39$request .= 'post_status="publish"
40AND comment_count > 0
41ORDER BY comment_count DESC LIMIT 0, ' . $postCount;
42 
43$posts = $wpdb->get_results($request);
44 
45if (count($posts) >= 1) {
46 
47if (!isset($args['title']) {
48$args['title'] = 'Popular Posts';
49}
50 
51foreach ($posts as $post) {
52wp_cache_add($post->ID, $post, 'posts');
53$popularPosts[] = array(
54'title' => stripslashes($post->post_title),
55'url' => get_permalink($post->ID),
56'comment_count' => $post->comment_count,
57);
58}
59 
60echo $args['before_widget'] . $args['before_title'] . $args['title'] . $args['after_title'];
61?>
62 
63<ol>
64<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
65foreach ($popularPosts as $post) {
66?>
67<li>
68php echo $post['url'];?>"><!--?php echo $post['title']; ?-->
69<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
70if ($displayComments) {
71?>
72(<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php echo $post['comment_count'] . ' ' . __('comments', BM_THEMENAME); ?>)
73<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
74}
75?>
76</li>
77<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
78}
79?>
80</ol>
81 
82<?php
83echo $args['after_widget'];
84}
85}
86?>

 

How to create wordpress widget plugin
How to create wordpress widget plugin

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.