Getting Wordpress Blog Authors in Combobox

shabbir's Avatar author of Getting Wordpress Blog Authors in Combobox
This is an article on Getting Wordpress Blog Authors in Combobox in Content Management System.

Introduction



I expected wordpress to have some built in support for listing all authors of the blog in a select box but that is not the case yet in Wordpress 2.9.2.

I was certain that I can get this done using wp_list_authors but actually it was a not that straight forward and so let me share this with you. I think this will help those looking for such functionality.

The Code



What I did is replaced the content of list items ( <li> ) into select box options ( <option> ) and added a JavaScript function to redirect user to the url when there is a change in the selection in the selection box.

So here is the code.
Code:
<select id="AuthorList" name="AuthorList" onchange="goAuthorList();">
	<?php 
	$authargs = array(
		'optioncount'   => false, 
		'exclude_admin' => false, 
		'show_fullname' => false,
		'hide_empty'    => true,
		'echo'          => false, // We will not output using the function.
		'feed'          => false, 
		'feed_image'    => false,
		'style'         => 'list',
		'html'          => true 
	); 
	$var_list = wp_list_authors( $authargs ); // Take the output into a variable.
	// Replace the tags of list to option box
	$var_list = str_replace("</a></li>","</option>",$var_list); 
	$var_list = str_replace("<li><a href=","<option value=",$var_list);
	// Now output the list
	echo $var_list;	
	?> 
</select>
<script language="JavaScript">
<!--
    function goAuthorList() {
	// JavaScript function to change the user to the url of the author
		theSelAuthor = document.getElementById("AuthorList").value;
			if ("" != theSelAuthor) location.href = theSelAuthor;
    }
//-->
</script>
Enjoy !!!
Pro contributor
12Mar2010,23:01   #2
hanleyhansen's Avatar
Cool!
Pro contributor
14Mar2010,07:54   #3
pankaj.sea's Avatar
Nice Info!
Go4Expert Founder
2Apr2010,09:11   #4
shabbir's Avatar
If you like this this article nominate it for Article of the month - Mar 2010
Go4Expert Founder
16Apr2010,19:44   #5
shabbir's Avatar
Vote for this article for Article of the month - Mar 2010
Contributor
30Jul2010,20:19   #6
johnny.dacu's Avatar
Why to list authors in a select? What's wrong with a list style?
Light Poster
5Apr2011,18:06   #7
rohan123's Avatar
Very Informative.
Thanks.
Banned
26Apr2011,14:51   #8
contusvideo's Avatar
Really nice info, Good contributory share @ shabbir!