0

I'm trying to create a dropdown of WordPress authors, with links to their author page.

Here is my current code:

<?php wp_dropdown_users(array('who'=>'authors')); ?>

I've got a similar dropdown for my archives, which is below (and works).

<select name="archive-dropdown" id="archives-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
<?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option','show_post_count' => 1 ) ); ?>
</select>

Right now, the authors just show under the dropdown, but they don't link to their pages. I tried adapting the archive code to my author code, but I don't get anything to work. Here's what I was trying, but it doesn't work:

<select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Author' ) ); ?></option> 
<?php wp_dropdown_users(array('who'=>'authors')); ?>
</select>               

I'm still pretty junior with my WordPress coding. Any insight would be great! I'm hoping I can help others if this gets solved for my situation.

Thanks!

1

I could be wrong as I don't know how Wordpress controls the dropdown menu but looks like it could be only returning a name of the author and not a link to their page.

I have tested the below code in a Wordpress site environment and works including linking to the authors page.

   <select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Author' ) ); ?></option> 
<?php 
// loop through the users
$users = get_users('role=author');
foreach ($users as $user) 
{
    if(count_user_posts( $user->id ) >0)
    {
      // We need to add our url to the authors page
      echo '<option value="'.get_author_posts_url( $user->id ).'">';
      // Display name of the auther you could use another like nice_name
      echo $user->display_name;
      echo '</option>'; 
    } 

}
?>
</select> 

working

Added update for making sure the authors have post and then added on administrator not sure how to combine the two roles without doing a duel foreach

<select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
    <option value=""><?php echo esc_attr( __( 'Select Author' ) ); ?></option> 
    <?php 
    // loop through the users
    $users = get_users('role=author');
    foreach ($users as $user) 
    {
        // get user who have posts only
        if(count_user_posts( $user->id ) >0)
        {
          // We need to add our url to the authors page
          echo '<option value="'.get_author_posts_url( $user->id ).'">';
          // Display name of the auther you could use another like nice_name
          echo $user->display_name;
          echo '</option>'; 
        } 

    }

$users = get_users('role=administrator');
    foreach ($users as $user) 
    {
        // get user who have posts only
        if(count_user_posts( $user->id ) >0)
        {
          // We need to add our url to the authors page
          echo '<option value="'.get_author_posts_url( $user->id ).'">';
          // Display name of the auther you could use another like nice_name
          echo $user->display_name;
          echo '</option>'; 
        } 

    }
    ?>
    </select> 
  • Thanks for posting, however it didn't work. I only get the "Select Author" in the dropdown. – gdeleon101 Mar 21 '17 at 21:15
  • Updated my code and works in my live site. – Sam Mar 21 '17 at 21:32
  • Your correct! It works! Thank you! But what if one of my administrators created a post? Can admins also show? Can we hide authors or admins that have zero posts? – gdeleon101 Mar 21 '17 at 21:48
  • I have added a check for making sure they have posts and then run the code for admins as I don't know how to adde them into one foreach loop. – Sam Mar 21 '17 at 22:09
  • Sam, I appreciate all your help and insight. I know others will benefit from this post. I did quite a bit of searching before I posted here. Thanks again! – gdeleon101 Mar 21 '17 at 22:22
0

A more "WordPress way" of doing it is to filter the output of the wp_dropdown_users() function instead of basically recreating the function.

In your theme or plugin, you want to add the filter immediately before you call wp_dropdown_users() and remove it immediately after to prevent unwanted side effects.

add_filter( 'wp_dropdown_users', 'wpse_260893_dropdown_users' );
wp_dropdown_users( [ 'who' => 'authors' ] );
remove_filter( 'wp_dropdown_users', 'wpse_260893_dropdown_users' );

Then you can place this in your plugin or theme functions.php.

//* Filter the output of wp_dropdown_users() function to replace user id with posts url
function wpse_260893_dropdown_users( $content ) {

  //* Find all cases where the option value is anything - (.*)
  $number_of_matches = preg_match_all( '/<option value=\'(.*)\'/', $content, $matches );

  //* If there's no matches, return early
  if( false === $number_of_matches || 0 === $number_of_matches ){
    return $content;
  }

  //* Get the author posts url for each of the matches found with preg_match_all()
  $posts_urls = array_map( function( $user_id ){
    return get_author_posts_url( $user_id );
  }, $matches[1] );

  //* Replace the author ids with their corresponding author posts url
  return str_replace( $matches[1], $posts_urls, $content );
}

Make sure to add the filter where you want to change the output of the wp_dropdown_users() function and remove it immediately after you don't need it anymore so that it doesn't affect anything else.

  • Nathan, thanks for the reply. Can you elaborate on what exactly the code is doing? I'm not so sure what it is doing. – gdeleon101 Mar 21 '17 at 23:03
  • Edited to hopefully make clearer what's going on. Cheers. – Nathan Johnson Mar 21 '17 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.