This tutorial will demonstrate the methods used to write a simple Posts By Author Widget plugin. The idea of this plugin is to provide a widget that can be used to displays posts from a specific author.

The plugin this tutorial is about is a much simplified version of the [highlight_green]Posts By Author Widget Pro plugin[/highlight_green].

[box style=”notice”]If you’re not familiar with how to make widgets, read this tutorial first.[/box]

The video quickly explains what is going on in the code below.

The Final Plugin Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
Plugin Name: Posts By Author Widget 
Plugin URI: https://pippinsplugins.com/simple-posts-by-author-widget-tutorial-and-plugin
Description: A Simple Widget to List Posts By a Specific Author
Version: 1.0
Author: Pippin Williamson
Author URI: https://pippinsplugins.com
*/
 
 
/**
 * Posts By Author Widget Class
 */
class pbaw_wrapper extends WP_Widget {
 
 
    /** constructor */
    function pbaw_wrapper() {
        parent::WP_Widget(false, $name = 'Posts By Author Widget', array('description' => 'Lists posts by a specific author') );	
    }
 
    /** @see WP_Widget::widget */
    function widget($args, $instance) {	
        extract( $args );
        $title 			= apply_filters('widget_title', $instance['title']);
        $author			= apply_filters('widget_title', $instance['author']);
        $number 		= apply_filters('widget_title', $instance['number']);
		$author_object 	= get_userdatabylogin($author);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                    echo $before_title . $title . $after_title; ?>
					<ul class="no-bullets">
					<?php
						global $post;
						$tmp_post = $post;
						$args = array( 'author' => $author_object->ID, 'numberposts' => $number);
						$myposts = get_posts( $args );
						foreach( $myposts as $post ) : setup_postdata($post); ?>
							<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
						<?php endforeach; ?>
						<?php $post = $tmp_post; ?>
					</ul>
              <?php echo $after_widget; ?>
        <?php
    }
 
    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] 			= strip_tags($new_instance['title']);
		$instance['author'] 		= strip_tags($new_instance['author']);
		$instance['number']	 		= strip_tags($new_instance['number']);
        return $instance;
    }
 
    /** @see WP_Widget::form */
    function form($instance) {	
 
		$posttypes = get_post_types('', 'objects');
 
        $title 			= esc_attr($instance['title']);
        $author			= esc_attr($instance['author']);
        $number 		= esc_attr($instance['number']);
        ?>
 
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:'); ?></label> 
          <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; ?>" />
        </p>
		<p>
			<label for="<?php echo $this->get_field_id('author'); ?>"><?php _e('Choose an Author'); ?></label> 
			<select name="<?php echo $this->get_field_name('author'); ?>" id="<?php echo $this->get_field_id('author'); ?>" class="widefat">
				<?php
				$authors = pbaw_get_authors();
				foreach ($authors as $a) {
					echo '<option value="' . $a . '" id="' . $a . '"', $a == $author ? ' selected="selected"' : '', '>', $a, '</option>';
				}
				?>
			</select>	
        </p>
		<p>
          <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:'); ?></label> 
        </p>
 
        <?php 
    }
 
 
} // class pbawp_wrapper
// register Posts By Author widget
add_action('widgets_init', create_function('', 'return register_widget("pbaw_wrapper");'));
 
 
/*
* Get all admins, editors, authors, and subscribers
* @Param - $email: set to true to retrieve an array of user emails
*/
function pbaw_get_authors($email = false){
    //Grab wp DB 
    global $wpdb;
    //Get all users in the DB
    $wp_user_search = $wpdb->get_results("SELECT ID, display_name, user_email FROM $wpdb->users ORDER BY ID");
 
    //Blank array
    $user_array = array();
    //Loop through all users
    foreach ( $wp_user_search as $userid ) {
        //Current user ID we are looping through
        $curID = $userid->ID;
        //Grab the user info of current ID
        $curuser = get_userdata($curID);
 
		// only get users that can edit posts
        if(user_can($curID, 'edit_posts')) {
			if($email == true) {
				//Push user email into array
				$user_array[] = $curuser->user_email;
			} else {
				//Push user ID into array
				$user_array[] = $curuser->user_login;
			}
        }
    }
    return $user_array;
}
Download Full Plugin

Leave a Reply