Creating a new appointment in the wp-admin > Appointments

When creating a new appointment in the wp-admin > Appointments > Add New, there is a drop-down field under "CLIENT" labeled "User". This field is a list of wordpress usernames. I have 3 questions regarding this field.

1) Is it possible to switch this to a list of Display Names rather than Usernames? Usernames can be totally different than the person's actual name. I have my WordPress set to show first_name last_name as the Display Name, which would be easier to identify the actual person we are creating the appointment for.

2) Is it possible to restrict the drop-down list to show only a particular role? For example, we created a custom role based on the subscriber role. The role is called patient. This is the only role that will need to make appointments on our site.

3) Is it possible to auto-populate the Name, Email, Phone, Address, City fields based on what is selected from this drop-down list? It would be best to auto-populate these fields dynamically as the menu item is selected in case there are two people with the same name, we could determine which is the person we are looking for by their other information. But if it can only auto-populate after the "Save/Update" button is pressed and a manual refresh is done, then it might be a good idea to leave the drop-down list as Username since each will always be unique. It would just require an extra step before making the appointment of finding the patient's username first.

Please let me know if any of these are possible and if so how?

Thanks

  • Patrick

    Hi there!

    I hope you're well today!

    For #1: I believe that is a feature that is currently being worked on, but has not yet been finalized. But this post should be of particular interest:
    https://premium.wpmudev.org/forums/topic/i-would-like-to-tweak-the-following-for-the-service#post-687545

    For #2: The function in the post I linked to above could be wrapped in a conditional to check for role, but not exactly sure how to code that. Would you need assistance with that part?

    For #3: It could be possible to integrate an Ajax refresh, but here again, that is beyond my level of expertise I'm afraid.

    I'll see if one of our 2nd-level wiz-kids can join in here to lend a hand with the custom coding bits. It might take a bit longer than a normal support ticket as they are a bit overloaded at the moment with code stuff.

  • Hoang Ngo

    Hi @STEVE,

    I hope you are well today.

    1) Is it possible to switch this to a list of Display Names rather than Usernames? Usernames can be totally different than the person's actual name. I have my WordPress set to show first_name last_name as the Display Name, which would be easier to identify the actual person we are creating the appointment for.

    We will need to update the code, in the file /appointments/appointments.php line 6676, you will see a line like this
    $html .= wp_dropdown_users( array( 'show_option_all'=>__('Not registered user','appointments'), 'show'=>'user_login', 'echo'=>0, 'selected' => $app->user, 'name'=>'user' ) );
    Please update to
    $html .= wp_dropdown_users( array( 'show_option_all'=>__('Not registered user','appointments'), 'show'=>'display_name', 'echo'=>0, 'selected' => $app->user, 'name'=>'user' ) );
    This will show by display name instead of login name

    2) Is it possible to restrict the drop-down list to show only a particular role? For example, we created a custom role based on the subscriber role. The role is called patient. This is the only role that will need to make appointments on our site.

    The function return the dropdown is wp_dropdown_users as you can see in the previous step, however, this not support for filter users by role, so we will need to create a new function for this. First, Please add this code to your theme functions.php or use mu-plugins

    function wp_custom_dropdown_users( $args = '' ) {
        $defaults = array(
            'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
            'orderby' => 'display_name', 'order' => 'ASC',
            'include' => '', 'exclude' => '', 'multi' => 0,
            'show' => 'display_name', 'echo' => 1,
            'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
            'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false,
            'option_none_value' => -1
        );
    
        $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
    
        $r = wp_parse_args( $args, $defaults );
        $show = $r['show'];
        $show_option_all = $r['show_option_all'];
        $show_option_none = $r['show_option_none'];
        $option_none_value = $r['option_none_value'];
    
        $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
        $query_args['fields'] = array( 'ID', 'user_login', $show );
        $query_args['role']='administrator';
        $users = get_users( $query_args );
    
        $output = '';
        if ( ! empty( $users ) && ( empty( $r['hide_if_only_one_author'] ) || count( $users ) > 1 ) ) {
            $name = esc_attr( $r['name'] );
            if ( $r['multi'] && ! $r['id'] ) {
                $id = '';
            } else {
                $id = $r['id'] ? " id='" . esc_attr( $r['id'] ) . "'" : " id='$name'";
            }
            $output = "<select name='{$name}'{$id} class='" . $r['class'] . "'>\n";
    
            if ( $show_option_all ) {
                $output .= "\t<option value='0'>$show_option_all</option>\n";
            }
    
            if ( $show_option_none ) {
                $_selected = selected( $option_none_value, $r['selected'], false );
                $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
            }
    
            $found_selected = false;
            foreach ( (array) $users as $user ) {
                $user->ID = (int) $user->ID;
                $_selected = selected( $user->ID, $r['selected'], false );
                if ( $_selected ) {
                    $found_selected = true;
                }
    
                $user_data = get_userdata($user->ID);
                $name = $user_data->first_name.' '.$user_data->last_name;
                $name = trim($name);
                if(strlen($name)>0){
                    $name = $name.' ('.$user_data->user_login.')';
                }else{
                    $name = $user->user_login;
                }
                $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
                $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $name ) . "</option>\n";
            }
    
            if ( $r['include_selected'] && ! $found_selected && ( $r['selected'] > 0 ) ) {
                $user = get_userdata( $r['selected'] );
                $name = $user->first_name.' '.$user->last_name;
                var_dump($name);
                $name = trim($name);
                if(strlen($name)>0){
                    $name = $name.' ('.$user->user_login.')';
                }else{
                    $name = $user->user_login;
                }
                $_selected = selected( $user->ID, $r['selected'], false );
                $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
                $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $name ) . "</option>\n";
            }
    
            $output .= "</select>";
        }
    
        /**
         * Filter the wp_dropdown_users() HTML output.
         *
         * @since 2.3.0
         *
         * @param string $output HTML output generated by wp_dropdown_users().
         */
        $html = apply_filters( 'wp_dropdown_users', $output );
    
        if ( $r['echo'] ) {
            echo $html;
        }
        return $html;
    }

    And next, please update the code in step 1 to
    wp_custom_dropdown_users( array('role'=>array('administrator'), 'show_option_all'=>__('Not registered user','appointments'), 'show'=>'display_name', 'echo'=>0, 'selected' => $app->user, 'name'=>'user' ) );
    What we change is instead of using the function wp_dropdown_users, we use our custom function for render the dropdown.
    You will note that, there's a line of code like this
    $query_args['role']='patient';
    Please update it to the right role name.

    3) Is it possible to auto-populate the Name, Email, Phone, Address, City fields based on what is selected from this drop-down list? It would be best to auto-populate these fields dynamically as the menu item is selected in case there are two people with the same name, we could determine which is the person we are looking for by their other information. But if it can only auto-populate after the "Save/Update" button is pressed and a manual refresh is done, then it might be a good idea to leave the drop-down list as Username since each will always be unique. It would just require an extra step before making the appointment of finding the patient's username first.

    Unfortunately this is a extremely complex feature and is beyond the scope of custom coding that we could provide. Also, for now the user dropdown will display user full name and user_login, so you will still know the right user for making an appointment.

    If you have any issues please don't hesitate to let us know so we can assist

    Best regards,
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.