Using Piklist you can easily display your registered users in a Checkbox, Dropdown (Select list) or as Radio Buttons. This simple tutorial will show you how:
- Install Piklist
- Decide if you are going to build your Piklist code as a plugin or in your theme.
- With Piklist you can display fields as Metaboxes, on a Settings page or a Widget. Choose which one you want to use.
Now we can get started and write some code! We’re are going to use the WordPress function get_users(), to get our users. This function stores data in an array, and with Piklist, parsing an array and displaying it as a Dropdown or Select List is really easy. As you can see from the WordPress Codex page, this array stores all users in an Object, and then breaks them out. You have access to fields like ID, user_login, user_pass, etc. We are going to assign each item in the dropdown to the ID, but show the display_name:
piklist('field', array( 'type' => 'select' ,'field' => 'my_users' ,'label' => 'Choose a user' ,'attributes' => array( 'class' => 'text' ) ,'choices' => piklist( get_users( array( 'orderby' => 'display_name' ,'order' => 'asc' ) ,'objects' ) ,array( 'ID' ,'display_name' ) ) ));
Let’s take a look at this code:
- First we created a standard Piklist Select list.
- The only major change is the choices array. We told Piklist to use the get_users() function, and then passed the orderby and order parameters to Piklist. These parameters are standard to the get_users() function and can be found at the Codex.
- The next array tells Pikist what should be the KEY for the field, and what should be the VALUE. In this case we selected ID as the KEY, and DISPLAY_NAME as the VALUE to show in the list.
To make this a Checkbox, just change ‘type’ => ‘select’, to ‘type’ => ‘checkbox’.
To make this Radio Buttons, just change ‘type’ => ‘select’, to ‘type’ => ‘radio’.
Yes, it’s that easy!