Change User Selection List Display

Hi

I would like to tweak the following - for the service provider within the settings is the ability to use the username as the drop down or First & Last Name from their profile.

I notice that the Client (User) field is a drop down in the admin area but displays only the username, can this be modified to again display as

First Name Last name (Custom Profile Field Club or Society)

The bit in brackets I would like to use a custom profile field that I have set up called Your Club or Society.

This will allow us in the admin to easily identify the client rather than trying to remember the username of the client when adding an appointment manually in the admin area.

Regards

Wayne

  • Patrick
    • Support Monkey

    Hey there @Wayne

    I hope you are having a great day!

    I believe the function used to fetch the client name is found in appointments/appointments.php around line 766.

    Exactly how to modify that or hook into it is a bit beyond my level of expertise. I'll flag our 2nd-level support team though to see if one of the wiz-kids can help out here.

  • Saurabh
    • New Recruit

    Hi @Wayne,

    The default attribute for the service providers can be filtered. add the following code in one of the custom plugins or functions.php:

    add_filter( 'app_get_worker_name', 'incsub_app_filter_worker_name', 10, 2 );
    
    function incsub_app_filter_worker_name( $user_name, $worker ){
    
        if( 0 != $worker){        
    
            $worker_details = get_userdata( $worker );
    
            if( $worker_details->first_name && $worker_details->last_name) {
    
                $user_name = $worker_details->first_name . " " . $worker_details->last_name;
    
            }
    
        }
    
        return $user_name;
    
    }

    In addition to the first name and the last name, you could use any of the user attributes listed here:

    http://codex.wordpress.org/Function_Reference/get_userdata

    Do let us know if this is sufficient.

  • Wayne
    • The Incredible Code Injector

    Hey @Saurabh Shukla

    Thanks for taking a look - I have placed that code in the bottom of the functions.php of my current theme but when I go to add a new appointment manually in the admin the list still shows the username of the customer as opposed to using the first and last name?

    Support access is granted.

    Regards

    Wayne

  • Saurabh
    • New Recruit

    Hi @Wayne,

    Hope you are well today.

    Sorry for the inconvenience. Those functions have changed in WordPress but they haven't updated the documentation. Looks like first name and last name are no longer available with standard user functions.

    I have made changes to the code, on your site and gotten this to work:

    add_filter( 'app_get_worker_name', 'incsub_app_filter_worker_name', 10, 2 );
    
    function incsub_app_filter_worker_name( $user_name, $worker ){
    
        if( 0 != $worker){        
    
            $last_name = get_user_meta( $worker, 'last_name', true );
    
    	$first_name = get_user_meta( $worker, 'first_name', true );
    
            if( $first_name && $last_name) {
    
                $user_name = $first_name . " " . $last_name;
    
            }
    
        }
    
        return $user_name;
    
    }

    Do let me know if this is satisfactory.

    Best

    Saurabh

  • Wayne
    • The Incredible Code Injector

    Hi @Saurabh Shukla

    Thank you - I am clicking on Add New in the admin area to create a new appointment however I am still seeing usernames. I am not seeing First Name Last Name

    Am I doing something wrong?

    Wayne

  • Saurabh
    • New Recruit

    Hi @Wayne,

    Looking at this again, I'm afraid it isn't possible without a core edit.

    At about line 8260 in appointments.php, we replace this code:

    $html .= wp_dropdown_users( array( 'show_option_all'=>__('Not registered user','appointments'), 'show'=>'user_login', 'echo'=>0, 'selected' => $app->user, 'name'=>'user' ) );

    with this:

    $html .= '<select name="user" id="user">';
    $html .= '<option value="0">'.__('Not registered user','appointments').'</option>';
    $users = get_users('fields=ID');
    foreach ($users as $id){
        $first_name = get_user_meta($id, 'first_name', true);
        $last_name = get_user_meta($id, 'last_name', true);
        $full_name = $first_name . " " . $last_name;
        $html .= '<option value="'.$id.'"'.selected( $id, $app->user, false).'>'.$full_name.'</option>';
    }
    $html .= '</select>';

    and this gets the dropdown to show first name and last name. The earlier code just changes the service provider dropdown. I have left that in place, as well.

    Best

    Saurabh

  • Saurabh
    • New Recruit

    Hi @Wayne,

    Glad we could help you. Please note that since we have made changes to the core, an update will break this feature. Even then, making these changes again will get the feature back.

    From a usability perspective, I agree that full names of users are much more helpful than usernames and such. I will mark this as a feature request so that we may consider this for future updates.

    Best

    Saurabh

  • Wayne
    • The Incredible Code Injector

    Hi @Saurabh Shukla

    Just noted one other area where it displays usernames as opposed to First Name Last Name and that is here

    /wp-admin/admin.php?page=app_settings&tab=workers

    How can this be changed also please?

    Support Access Granted as always.

    Thanks

    Wayne

  • Ash
    • WordPress Hacker

    Hi @Wayne

    You can't show first name - last name in here, but you can show display name in there.

    To show display name, please go to /wp-content/plugins/appointments/appointments.php line no 7140 and replace

    $workers = wp_dropdown_users( array( 'echo'=>0, 'show'=>'user_login', 'selected' => $worker->ID, 'name'=>'workers['.$k.'][user]', 'exclude'=>apply_filters('app_filter_providers', null) ) );

    to

    $workers = wp_dropdown_users( array( 'echo'=>0, 'show'=>'display_name', 'selected' => $worker->ID, 'name'=>'workers['.$k.'][user]', 'exclude'=>apply_filters('app_filter_providers', null) ) );

    And line 7148

    $workers =str_replace( array("\t","\n","\r"), "", str_replace( array("'", "'"), array('"', "'"), wp_dropdown_users( array ( 'echo'=>0, 'show'=>'user_login', 'include'=>0, 'name'=>'workers['.$k.'][user]', 'exclude'=>apply_filters('app_filter_providers', null)) ) ) );

    to

    $workers =str_replace( array("\t","\n","\r"), "", str_replace( array("'", "'"), array('"', "'"), wp_dropdown_users( array ( 'echo'=>0, 'show'=>'display_name', 'include'=>0, 'name'=>'workers['.$k.'][user]', 'exclude'=>apply_filters('app_filter_providers', null)) ) ) );

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.