List site contributors the WordPress way

Posted on Leave a comment
5/5 - (210 votes)

Render unto Caesar the things that are Caesar’s, and unto God the things that are God’s
—Jesus Christ (of Nazareth)

For brevity’s sake, I’m certain, “render unto the author what is his” was left out of the original synoptic gospel quote attributed to J.C. of Nazareth. R.I.P, by the way. It was implied, obviously, right?

Bedtime stories aside, if you’re building a theme for anything close to a magazine site, you’ll find that all contributing authors will want to be credited. Both in the expected byline, along with their article but also on a dedicated “contributors page” or equivalent. Its simply the nature of humans and multi-author sites.

One approach to customizing the fields and possibilities available on this page is to define a “Contributor” custom post type. Here, you can define an endless amount of fields in which a contributor’s information can be filled in and displayed.

Take a minute with that thought, however, and you’ll quickly realize that this approach will just end up being a pointless duplication of the already endlessly extensible functionality present in the core Users functionality.

In the preceding section, you defined (and removed) a few extra fields for users. How then do you display those fields on the front-end and beyond that, how do you limit what users are displayed? e.g. what if you only want to display information for users who have contributed/written at least one post. How do you do that? Or for just users with an “administrator” level of access. How do you go about that?

Show me the users!

The get_users() core function is your buddy here. This function, given a set of arguments, will retrieve the needed set of matching users. For the sake of this example, lets say that you have a theme for a magazine site. On this site, there is a page that outputs a list of authors. Not all authors, though, just the ones that are pulling their own weight; that have contributed to said magazine.

Defining the function

First we declare a function butter_contributing_authors(). Then, we have get_users() grab and stash the contributors’ info in a variable $contributors for use later:

function butter_contributing_authors() {
  $contributors = get_users( array(
    'fields' => 'all',
    'orderby' => 'post_count',
    'order' => 'DESC',
    'who' => 'authors',
  ));
}

The ‘fields’ argument defaults to ‘all’ so technically we can leave this out but for the sake of illustration, we’ll leave it in place. This grabs all the specific contributor/user’s user_fields.

Depending on your implementation, ‘orderby’ sorts the users by ‘ID’, ‘login’, ‘nicename’, ’email’, ‘url’, ‘registered’, ‘display_name’, or ‘post_count’. Assuming a magazine theme, you might want to give precedence/prominence to the highest contributors. This example orders the contributors based on the count of posts they’ve authored in descending order (DESC: defined in ‘order’).

With this in place, WordPress can grab users and order them accordingly. To grab only users whose post count is greater than 1 and also define the markup including the added contact methods (that we defined in the User Fields chapter), here’s how the full function ought to look like:

function butter_contributing_authors() {
  $contributors = get_users( array(
    'fields'  => 'all',
    'orderby' => 'post_count',
    'order'   => 'DESC',
    'who'     => 'authors',
  ) );

  foreach ( $contributors as $contributor ) :
    $post_count = count_user_posts( $contributor );

    // Move on if user has not published a post (yet).
    if ( ! $post_count ) {
      continue;
    }
  ?>

  <a href="<?php echo esc_url( get_author_posts_url( $contributor ) ); ?>">
    <figure>
      <?php echo get_avatar( $contributor, 132 ); ?>
      <figcaption>
        <h2><?php echo get_the_author_meta( 'display_name', $contributor ); ?></h2>
        <p><?php echo get_the_author_meta( 'description', $contributor ); ?></p>
        <p><?php printf( _n( '%d Article', '%d Articles', $post_count, 'butter' ), $post_count ); ?></p>
        </figcaption>
    </figure>
  </a>

  /* 
  definition list of usernames (typically you’ll want to define actual links and maybe icons for social networks) as defined in the preceding section.
  */

  <dl>

    <dt><?php __('Skype', 'butter'); ?></dt>
    <dd><?php echo get_the_author_meta( 'skype', $contributor_id ); ?></dd>

    <dt><?php __('Twitter', 'butter'); ?></dt>
    <dd><?php echo get_the_author_meta( 'twitter', $contributor_id ); ?></dd>

    <dt><?php __('Dribbble', 'butter'); ?></dt>
    <dd><?php echo get_the_author_meta( 'dribbble', $contributor_id ); ?></dd>

    <dt><?php __('Facebook', 'butter'); ?></dt>
    <dd><?php echo get_the_author_meta( 'facebook', $contributor_id ); ?></dd>

  </dl>

  <?php endforeach;
}
endif;

Using the function

With the above function in place we can now list out our contributing authors. All you need to do is create a page template named, e.g. contributors.php. Make sure to set it up as a template i.e. make sure you have the requisite code comment at the top to render it as a page template. i.e.:

<?php
/*
 * Template Name: Contributor Page
*/

Inside a loop on this page, now all you need to do now is call the previously defined function:

<?php butter_contributing_authors(); ?>

Done! The list of contributors authors will thus be output here as defined by the function.

Make it pluggable; be nice to the children

As with such functions, you should consider making this function pluggable i.e. accessible and overwritable by child themes. This is easy. Just make sure you wrap it in an if ( ! function_exists() ) and you should be good. i.e.

if ( ! function_exists( 'butter_contributing_authors' ) ) :
  function butter_contributing_authors() {
  // function as defined in above steps
  }
endif;
Leave a Reply

Your email address will not be published. Required fields are marked *