Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Creating a Short Code to Show a Detailed List of Blog Authors

Posted on February 9, 2012 by Pippin in Intermediate, Short Codes, Tutorials, Working with Users 22 Comments
Home» Tutorials » Intermediate » Creating a Short Code to Show a Detailed List of Blog Authors
Tweet
Love It - 0

Since WordPress makes it so easy to run a multi-author blog, one of the things site administrators often want to do is have a page that shows all of the contributing authors of the blog. This could of course be done with a regular WordPress page, but that becomes much more difficult to update and maintain, so we want to automate it. This tutorial will show you how to create a simple short code that output a list of all authors on the site, including their avatars, name, biographies, and links to their post archives.

First, we start by setting up our plugin header:

1
2
3
4
5
6
7
8
9
/*
Plugin Name: Author Archive Short Code
Plugin URI: http://pippinsplugins.com/creating-a-short-code-to-show-a-detailed-list-of-blog-authors
Description: Adds a short code that allows you to display a list of all authors on your site, along with their gravatars, bios and author archive links
Author: Pippin Williamson
Contributors: mordauk
Author URI: http://pippinsplugins.com
Version: 1.0
*/

Once that’s done, we write the shell of our function that will setup and register our short code.

1
2
3
4
function pippin_list_authors() {
   // code will go here
}
add_shortcode('authors', 'pippin_list_authors');

Inside of this function, we go and retrieve a list of all of the authors using the get_users() function.

1
2
3
4
5
6
7
$authors = get_users(array(
		'orderby' => 'display_name',
		'count_totals' => false,
		'who' => 'authors'
 
	)
);

This will retrieve all authors ordered by their display name.

Next we setup our $list variable, which will be a string that contains all of our HTML for the author list.

1
2
3
4
5
6
7
8
9
10
11
$list = '';
 
if($authors) :
 
	$list .= '<ul class="author-list">';
		 // will loop through each o the authors here
	$list .= '</ul>';
 
endif;
 
return $list;

Inside of our unordered list, we need to loop through the array of authors we retrieved with the get_users() function so that we can show the details for each authors. This is done like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
foreach($authors as $author) :
 
	$list .= '<li class="author">';
 
		$archive_url = get_author_posts_url($author->ID);
 
		$list .= get_avatar($author->user_email, 60);
 
		$list .= '<a href="'. $archive_url . '" title="' . __('View all posts by ', 'pippin') . $author->display_name . '">' . $author->display_name . '</a>';
 
		$list .= '<p class="author-bio">' . get_user_meta($author->ID, 'description', true) . '</p>';
 
		$list .= '<p class="author-archive"><a href="'. $archive_url . '" title="' . __('View all posts by ', 'pippin') . $author->display_name . '">' . __('View author\'s posts', 'pippin') . '</a></p>';
 
	$list .= '</li>';
 
endforeach;

To get the information for each author in the foreach loop, we are using a few very handy functions:

  • get_author_posts_url() – retrieves the URL to the author’s post archive
  • get_avatar() – retrieves the author’s image set via gravatar.com
  • get_user_meta() – retrieves meta data for the user, in this case the author’s biography

And that’s it. That is all we need to show a list of our authors along with their images, names, descriptions, and archive links.

The only thing we have left is styling. To make our list look a little better, let’s load a stylesheet using the wp_enqueue_style() function.

1
wp_enqueue_style('author-list', plugin_dir_url(__FILE__) . '/css/author-list.css');

Since WordPress 3.3+ allows inline style and script loading, we can place this function directly into our short code function. So all together, our function looks like this:

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
/*
Plugin Name: Author Archive Short Code
Plugin URI: http://pippinsplugins.com/creating-a-short-code-to-show-a-detailed-list-of-blog-authors
Description: Adds a short code that allows you to display a list of all authors on your site, along with their gravatars, bios and author archive links
Author: Pippin Williamson
Contributors: mordauk
Author URI: http://pippinsplugins.com
Version: 1.0
*/
 
function pippin_list_authors() {
 
	$authors = get_users(array(
			'orderby' => 'display_name',
			'count_totals' => false,
			'who' => 'authors'
 
		)
	);
 
	$list = '';
 
	if($authors) :
 
		wp_enqueue_style('author-list', plugin_dir_url(__FILE__) . '/css/author-list.css');
 
		$list .= '<ul class="author-list">';
 
			foreach($authors as $author) :
 
				$list .= '<li class="author">';
 
					$archive_url = get_author_posts_url($author->ID);
 
					$list .= get_avatar($author->user_email, 60);
 
					$list .= '<a href="'. $archive_url . '" title="' . __('View all posts by ', 'pippin') . $author->display_name . '">' . $author->display_name . '</a>';
 
					$list .= '<p class="author-bio">' . get_user_meta($author->ID, 'description', true) . '</p>';
 
					$list .= '<p class="author-archive"><a href="'. $archive_url . '" title="' . __('View all posts by ', 'pippin') . $author->display_name . '">' . __('View author\'s posts', 'pippin') . '</a></p>';
 
				$list .= '</li>';
 
			endforeach;
 
		$list .= '</ul>';
 
	endif;
 
	return $list;
}
add_shortcode('authors', 'pippin_list_authors');

And to improve our appearance, our stylesheet has the following styles:

1
2
3
4
5
6
.author-list { margin: 0; padding: 0; }
.author-list li { display: block; margin: 0 0 20px 0;}
.author-list li:after { display: block; float: none; height: 0; visibility: hidden; content: "."; clear: both; }
.author-list img { float: left; margin: 0 10px 10px 0;}
.author-list .author-bio { margin: 0; }
.author-list .author-archive { float: right; }

Once you’ve placed the [authors] short code on a page, your result should look about like this:

You can download the complete plugin below.

Download Plugin
Related Items
  • Plugin Development 101 – Intro to Short Codes
Tweet Follow @pippinsplugins
add_shortcode, get_avatar, get_users

22 comments on “Creating a Short Code to Show a Detailed List of Blog Authors”

  1. Justin Kopepasah says:
    February 9, 2012 at 5:15 pm

    I am just curious… why would you not just create a template file within the theme and select this file on the page?

    Reply
    • Pippin says:
      February 9, 2012 at 5:18 pm

      Great question. If you’re building a theme, then you could most definitely do that, but what if you’re not the person who developed the theme? This would allow you to do the same thing with any page. The method used in this short code also applies to themes, so you could use this exact same code to build a template in your theme.

  2. drewmcmanus says:
    February 9, 2012 at 5:24 pm

    Fantastic tut Pippin, can’t wait to start playing around with it tomorrow! I wasn’t even thinking about making them shortcodes but that is a fantastic idea

    Reply
    • Pippin says:
      February 9, 2012 at 5:26 pm

      Nice about this function is it can be echoed in a template file as well.

    • drewmcmanus says:
      February 9, 2012 at 5:40 pm

      Couldn’t wait until tomorrow and with a little display:none work, put together a very simply list: http://www.adaptistration.com/tafto/contributor-archive/

    • Pippin says:
      February 9, 2012 at 5:45 pm

      Nice!

  3. Paul says:
    February 10, 2012 at 6:08 am

    actually I think the ability to load scripts and styles inline in shortcodes is pretty important, because it means plugins have no excuse to load their scripts on every page anymore.

    Reply
    • Pippin says:
      February 10, 2012 at 7:43 am

      Yeah, definitely! It’s one of the 3.3 features I’m only really starting to use, but it’s awesome. Gone are the days of using three different functions in order to get scripts to load only when the short code is present.

  4. ed says:
    February 12, 2012 at 8:26 am

    Great Tut! Playing around with it now! Would love to add authors twitter, facebook, etc links.

    Reply
  5. muntzdesigns says:
    February 12, 2012 at 9:21 am

    I guess my only question is how can you remove some some of the admin users from the list?

    Reply
    • muntzdesigns says:
      February 12, 2012 at 10:10 am

      I just added the ‘exclude’ => array(id#),

      This worked great!

    • Pippin says:
      February 12, 2012 at 7:49 pm

      Yep, that’s how :)

  6. Susan says:
    February 17, 2012 at 10:16 pm

    Great! I can’t wait to start playing with it, this weekend!

    Reply
    • Pippin says:
      February 18, 2012 at 12:27 pm

      Let me know if you have any questions!

  7. Rob Moore says:
    June 5, 2012 at 10:27 pm

    hi,
    I’m trying to exclude specific authors in the list. I see that there was mention of using an exclude but not sure exactly where in the code to put that. For purposes, I want to exclude authors with the id of 259 and 14. Can anyone give me the revised code for that?

    Thanks,
    Rob

    Reply
    • Pippin says:
      June 6, 2012 at 3:10 pm

      Inside of the get_users() function, add this:

      'exclude' => array(259,14)
  8. Rob Moore says:
    September 13, 2012 at 8:26 am

    How can I make the orderby sort the authors by their last name instead of their display name.

    Right now it is:
    $authors = get_users(array(
    ‘orderby’ => ‘display_name’,
    ‘count_totals’ => false,
    ‘exclude’ => array(1,277,),
    ‘who’ => ‘authors’

    Reply
    • Pippin says:
      September 13, 2012 at 12:10 pm

      It’s actually not possible to sort by the last name, not without building your own custom sorting options.

  9. Marcus Pessoa says:
    February 13, 2013 at 12:10 am

    Thank tou very much!!!!
    I was looking for a simple plug-in like this to my newest creative blog! Can we give you 5 stars for this plug in? Where’s the link on wp? :D

    btw my blog’s link is http://www.labcriativo.com.br

    Reply
  10. Tarek says:
    May 12, 2013 at 3:42 am

    Pippin,
    Thank you so much for this plugin.. I was looking for this a long time ago and I just found it here. Exactly the way I wanted it, and very simple programming.. Great job buddy!

    Reply
  11. Non says:
    June 17, 2013 at 3:47 am

    Hi, is there any way how teach this plugin to work with Co Author plus plugin?
    Thanks

    Reply
    • Pippin says:
      June 18, 2013 at 9:10 pm

      No, sorry.

Leave a Reply Cancel 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>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Stripe Integration Part 9 – The Stripe Button
  • Custom Front End Post Submission Form
  • User Submitted Image Gallery – Part 5

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

campaign monitor add_options_page hook get_user_meta Rémi Corson the_content meta box register_setting Sugar Event Calendar attachments wp_enqueue_script contextual help shortcodes authors Related posts attachment image forms do_action plugin mail chimp login short codes comments recent posts post types apply_filters short code bbpress taxonomies custom post type gallery images Ajax Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins