2

I'm trying to make a list of users come up in a widget in the admin dashboard. So far i've got it showing up, but I want the list to show a custom field from the user's profile instead of the normal display_name or ID.

My code so far:

    function wps_userlist_dw() {

wp_dropdown_users(array('name' => 'author',
         'exclude'                 => '1',
         'name'                    => 'author',
         'selected'                 => $user_id,
         'show'                    => 'ID'
    ));

}
function add_wps_userlist_dw() {
       wp_add_dashboard_widget( 'wps_userlist_dw', __( 'Select a User Below' ), 'wps_userlist_dw' );
}
add_action('wp_dashboard_setup', 'add_wps_userlist_dw' );

So instead of ID, i need a custom field (made via Cimy Extra User Fields - http://wordpress.org/plugins/cimy-user-extra-fields/). Lets say the custom field's name that i want to show is 'COMPANYNAME'.

Any thoughts? If i put in 'COMPANYNAME' in the 'show' value, the whole function doesn't show up.

Thanks!!

1 Answer 1

1

wp_dropdown_users accept for show arguments only field from the users table, not from user_meta table. If you enable debug, with your code, using 'companyname' for $show argument you will see something like:

WordPress database error: [Unknown column 'wp_users.companyname' in 'field list'] SELECT wp_users.ID,wp_users.user_login,wp_users.companyname FROM wp_users WHERE 1=1 ORDER BY display_name ASC

So, solution is create your customized wp_dropdown_users function (wp_dropdown_users_extended?) just copy the code from the original function and modify it.

Tip: What you need is a function that to retierve users (line 1035 of wp function) use a WP_User_Query (docs) that run a meta query if $show is not one of the field in users table, and then use this meta on the output.

Probably the function should start like so:

function wp_dropdown_users_extended( $args = array() ) {
  $std_fields = array('ID', 'user_login', 'user_nicename', 'user_email', 'user_url', 'display_name');
  if ( isset($args['show']) && in_array($args['show'], $std_fields) ) {
    if ( isset($args['echo'] && ! $args['echo'] )
      return wp_dropdown_users($args);
    else
      wp_dropdown_users($args);
  } else {

    // copy here the code from wp_dropdown_users and customize it ;)    

  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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