Getting Wordpress Blog Authors in Combobox

Discussion in 'Content Management System' started by shabbir, Mar 12, 2010.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    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 !!!
     
  2. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    http://www.hanseninfotech.com
  3. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  6. johnny.dacu

    johnny.dacu New Member

    Joined:
    Jul 6, 2010
    Messages:
    88
    Likes Received:
    4
    Trophy Points:
    0
    Why to list authors in a select? What's wrong with a list style?
     
  7. rohan123

    rohan123 New Member

    Joined:
    Apr 5, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Very Informative.
    Thanks.
     
  8. contusvideo

    contusvideo Banned

    Joined:
    Apr 25, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.hdflvplayer.net/
    :seeya:Really nice info, Good contributory share :detective @ shabbir! :2thumbsup
     
  9. Nan

    Nan New Member

    Joined:
    Nov 19, 2020
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Thank you! Since I am a beginner to WordPress, the code was really helpful for me to add an Author's Info in to Box
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice