How to Display a List of Authors in a Dropdown Menu using wp_dropdown_users

Home » Snippets » How to Display a List of Authors in a Dropdown Menu using wp_dropdown_users
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to create a dropdown menu in WordPress that displays a list of authors? WordPress provides a function called wp_dropdown_users that makes it easy to generate a dropdown menu of users. In this article, we will guide you through the process of using wp_dropdown_users to display a list of authors in a dropdown menu. Whether you want to showcase the authors on your blog or create a custom author selection feature, this tutorial will give you the step-by-step instructions you need. Let’s dive in and start building your dropdown menu of authors in WordPress.

					function wpturbo_display_authors_dropdown() {
    $args = array(
        'orderby'       => 'display_name',
        'order'         => 'ASC',
        'who'           => 'authors',
        'name'          => 'wpturbo_author',
        'selected'      => get_query_var( 'author' ),
        'show_option_none' => __( 'Select Author', 'wpturbo' ),
        'echo'          => false,
    );
    
    $authors_dropdown = wp_dropdown_users( $args );
    
    echo $authors_dropdown;
}
add_shortcode( 'wpturbo_authors_dropdown', 'wpturbo_display_authors_dropdown' );
				

The code snippet provided is an example of how to display a dropdown menu with a list of authors in WordPress using the wp_dropdown_users() function. This can be useful, for example, if you want to allow users to filter content based on the author.

Let’s go through the code step by step to understand how it works.

The first step is to define a new function called wpturbo_display_authors_dropdown(). This function will be responsible for generating the HTML code for the dropdown menu.

Inside the function, we set up an array called $args that contains various parameters for customizing the behavior and appearance of the dropdown menu. Here’s a breakdown of each parameter:

  • orderby and order: These parameters set the order in which the authors will be displayed in the dropdown menu. In this example, they are set to display the authors in ascending order based on their display names.

  • who: This parameter specifies the type of users to include in the dropdown menu. In this case, we set it to "authors" to include only users with the "Author" role.

  • name: This parameter sets the name attribute for the HTML select element. It is used to identify the selected option when the form containing the dropdown menu is submitted. In this example, it is set to "wpturbo_author".

  • selected: This parameter sets the default selected option in the dropdown menu. In this case, we use the get_query_var() function to get the current author ID from the query string and select the corresponding option in the dropdown menu. This allows us to preserve the selected author when the page is refreshed.

  • show_option_none: This parameter sets the text for the default "Select Author" option in the dropdown menu. It is used as the first option in the menu to indicate that no specific author is selected.

  • echo: This parameter determines whether the wp_dropdown_users() function should output the HTML code directly or return it as a string. In this case, it is set to false so that we can assign the generated HTML code to the variable $authors_dropdown.

Next, we call the wp_dropdown_users() function passing the $args array as the parameter. This function generates the HTML code for the dropdown menu based on the provided arguments.

We assign the generated HTML code to the variable $authors_dropdown.

Finally, we use the echo statement to output the $authors_dropdown variable, which contains the HTML code for the dropdown menu.

To make this feature available as a shortcode, we use the add_shortcode() function to map the wpturbo_display_authors_dropdown() function to the shortcode [wpturbo_authors_dropdown]. This allows us to easily embed the dropdown menu in posts or pages by using the shortcode.

Overall, this code snippet provides a simple and efficient way to display a dropdown menu with a list of authors in WordPress.

Register an account to save your snippets or go Pro to get more features.