Blog Pile, Wordpress

Coding My Own WordPress Authors List


cc licensed flickr photo shared by bitzcelt

Elwood: It’s 106 miles to get this sidebar coded, we’ve got a full tank of gas, half a pack of cigarettes, it’s dark and we’re wearing sunglasses.
Jake: Hit it.

I have no idea why I opened this way except for Jim Groom Inspiration.

But to jive my code chops, there’s nothing more energizing than doing a little hack and chop coding in WordPress.

Today I was again doing some sidebar fine tuning on the NMC MIDEA web site (previously covered in my almost done series on doing custom post types in WordPress 3.0).

It was one of those “oh this might take 20 minute” deals that ended up going a bit longer. but like Jake and Elwood, when you gotta go to Chicago, you drop the sunglasses and hit it.

And this is pretty simple, and most likely there is a plugin that already does what I needed, but sometimes, it is just worth the effort to roll your own code. It’s the same joy of actually doing something in your car engine that actually does not result in flames or calling a tow truck.

What we have on this site is a number of accounts, me as admin, some staff as editors, and a few guest bloggers, with roles as authors. The category our gues authors blog under is called “Ideas“, and my idea was to add to the template sidebar, above the part where widgetized stuff happens, a blurb and a listing of our guest bloggers. I also liked the feature of not listing authors who have yet to post.

At first it seemed like the function wp_list_authors would do, but it has no way to list just authors of a certain role- it is meant for all authors (you can hide the administrator; I thought by making all our staff admins it would work, but that did not).

I scoured a few plugins, but most are widget oriented (its a long explanation, but because this only appears on category archives, doing a special set of widgets was overload) or or geared for listing author avatars.

Next I googled and found a custom function from WP Engineer that at first looked good, but it was written for previous versions of WordPress- things have really changed from the early days of basing author capabilities on an integer user role number, though the info is still there in the database. I started editing their script to get it to work, going back and forth to phpMyAdmin to run test queries.

it ended up sort of working, but still was not what I sought. I wanted it (a) to not only list the authors, but link to their profiles; (b) list the number of posts the author has written like the wp_list_authors() function does; (c) skip authors that have not posted (again like wp_list_authors() function does); and (d) list the authors in alphabetical order of their last name… which I did not find anywhere.

With some more elbow grease I more or less got it going, but really though the code was not quite as lean as it should be (this is the place where it may not pay to keep coding, but now Jake and Elwood were only 50 miles from the Windy City, and they could see the glow of light on the horizon).

There is actually quite a bit of fine grained tuning you can do for blog user accounts, down to the specific capabilities, and you can add/remove what accounts can do beyond the basic titles of “author” editor (see Roles and Capabilities). Most of this seems put to use for creators of plugins.

But with another round of Googling, I found the function I was looking for from Steve Taylor– it would return a list of ids of users that had a given role, and it made my code a lot shorter.

Only 25 miles til Chi-Town, boys….

I still ended up rolling some bits to do the parts I wanted… so here are the two functions I added to my functions.php template to generate a list of users with a given role as a >ul<…>/ul&lt list, in alphabetical order by last name (I made all the accounts, so I know they have these parts completed), skipping users with no posts…

It is pretty heavily commented, and ought to be semi-explanatory…

And this is simply called in my main sidebar.php template… I use a conditional to insert it where I want, so it only appears on the archives for the “ideas” category (I made the defaults of my function match my use case… sue me!)

And with that, welcome to the big city… see it in action

And the boys are now in town!


cc licensed flickr photo shared by cszar

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as @cogdog@cosocial.ca

Comments

  1. Fantastic article and code. I used it to create an author plugin for my blog and it worked like a charm.

    I changed it to work as a shortcode and made a few other improvements.

    It is a good idea to not have the post count as part of the link. It looks nicer and is better for SEO. So I changed that.

    Once the plugin is installed you can just add a shortcode to any post and it generates a list of authors. You can specify the role type in the short code like this:

    [authors role=”contributor”]

    Blog editor note… The code was sent to me via email.

    You can find the sample code I used for my own web site at http://cogdogblog.com/code/show-authors.php.txt — you can update the plugin-code header to match your own site needs.

  2. Thanks for this! 🙂 However, when I try the plugin, it seems to show only the first and last registered users of a particular role rather than the entire list. Am I missing something?

  3. Hi, this all looks good, but wondering if you could suggest some tweaks to achieve what I’m after. I’d like to have one page that shows all authors with their user photo, bio and link to their individual author page, and another page that shows the same info for all contributors…

    1. Its certainly possible if you have the PHP chops and WP template awareness. A custom authors list is a matter of editing the structure inside the loop if your authors.php template to get the content what you want, and formatted.

      A contributors page? I would go about that by creating a page with the intro text you want, and then create a page template that loops through the users. You’d have to likely run a custom WP query to get the users with the role you want. The code above should be a good start.

      If I spelled it out further, I’d be coding it!

  4. Just used this to make a count of events that authors have submitted for the rvarts.org site through The Events Calendar plugin. The fun part is I ended up here through something else and I was like “I know that guy!”

    Thanks.

Leave a Reply

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