Display two or more Additional Fields in the All Appointments table

Hi,

I have created two Additional fields in the admin by activating the "Additional Fields" add on. I have called the fields Membership Number and Membership Name.

I wish to display both the fields in the All Appointments table, after each other.

I have added the following code in the functions.php but only the first column displays. The problem is because the add_filter('app-shortcode-all_appointments-after_date','incsub_appt_add_field_display_field_value_name',99,2); cannot work the second time with the same class ('app-shortcode-all_appointments-after_date'

I have tested by changing order of columns and it works ok.

Is there a way around this please?

Code follows:

/**
Members Number
**/
add_filter('app_all_appointments_column_name','incsub_appt_add_field_display_col_membership_number');
function incsub_appt_add_field_display_col_membership_number($head_html){
	// Repeat the following line for headers/column names
	$head_html .= '<th class="all-appointments-membership">' . __('Membership. No.', 'appointments' ) . '</th>';

	return $head_html;
}

add_filter('app-shortcode-all_appointments-after_date','incsub_appt_add_field_display_field_value',99,2);
function incsub_appt_add_field_display_field_value($x,$appointment){
	$appointments_data = get_option('appointments_data', array());

	$extra_html = '';
	$extra_fields = empty($appointments_data[$appointment->ID])? array(): $appointments_data[$appointment->ID];

	$extra_html .= '<td>'.$extra_fields['membershipnumber'].'</td>';

	return $extra_html;
}
/**
Members Name
**/
add_filter('app_all_appointments_column_name','incsub_appt_add_field_display_col_member_name');
function incsub_appt_add_field_display_col_member_name($head_html){
	// Repeat the following line for headers/column names
	$head_html .= '<th class="all-appointments-name">' . __('Name', 'appointments' ) . '</th>';

	return $head_html;
}

//  THIS IS THE LINE THAT BREAKS
add_filter('app-shortcode-all_appointments-after_date','incsub_appt_add_field_display_field_value_name',99,2);
function incsub_appt_add_field_display_field_value_name($x,$appointment){
	$appointments_data = get_option('appointments_data', array());

	$extra_html = '';
	$extra_fields = empty($appointments_data[$appointment->ID])? array(): $appointments_data[$appointment->ID];

	//repeat the following line for each extra fields, replacing hometeam with appropriate values
	$extra_html .= '<td>'.$extra_fields['membername'].'</td>';

	return $extra_html;
}
  • Michael Bissett

    Hey @Ellis,

    After working with my colleague @Jose on this, we've got some code for you that you can use (you'll have to replace what you presently have though):

    function to_clean_name ($label) {
        $clean = preg_replace('/[^-_a-z0-9]/', '', strtolower($label));
        if (empty($clean)) $clean = substr(md5($label), 0, 8);
        return $clean;
    }
    
    function get_appointment_meta ($appointment_id) {
        $appointments_data = get_option('appointments_data', array());
        return empty($appointments_data[$appointment_id])
            ? array()
            : $appointments_data[$appointment_id]
            ;
    }
    
    function render_column_headers($table_headers) {
        global $appointments;
        $app_data = $appointments->options;
        $fields = !empty($app_data['additional_fields']) ? $app_data['additional_fields'] : array();
        if (empty($fields)) return $table_headers;
    
        foreach ($fields as $field) {
            $label = esc_html($field['label']);
            $name = to_clean_name($label);
    
            $table_headers .= '<th class="all-appointments-status">' . __($name, 'appointments' ) . '</th>';
        }
        return $table_headers;
    }
    
    add_filter('app_all_appointments_column_name', 'render_column_headers', 10, 1);
    
    function render_additional_cell($cell, $app) {
        global $appointments;
        $app_data = $appointments->options;
        $fields = !empty($app_data['additional_fields']) ? $app_data['additional_fields'] : array();
        if (empty($fields)) return $cell;
    
        $app_meta = get_appointment_meta($app->ID);
        if (empty($app_meta)) return $cell;
    
        foreach ($fields as $field) {
            $label = esc_html($field['label']);
            $name = to_clean_name($label);
            $value = !empty($app_meta[$name]) ? esc_attr($app_meta[$name]) : '';
    
            $cell .= '<td>' . $value . '</td>';
        }
        return $cell;
    }
    
    add_filter('app_all_appointments_add_cell', 'render_additional_cell', 10, 2);

    Hope this helps! :slight_smile:

    Regards,
    Michael

  • Michael Bissett

    Hey @Ellis,

    Glad to hear that it's working for you! :slight_smile:

    1. Can I change the heading names as it appears I get technical names (membershipnumber , membershipname)?

    It's rendering the names you entered for the fields, so for this, I would recommend modifying the names you've entered here.

    2. Can I remove one as I have another Additional Field I do not want displayed?

    I'll have to call in my colleague @Jose for this one, as this is more so his speciality. :slight_smile:

    Regards,
    Michael

  • Jose

    Hey there @Ellis,

    This is the code with some modifications to allow you to select what additional fields will render in the table:

    function to_clean_name ($label) {
        $clean = preg_replace('/[^-_a-z0-9]/', '', strtolower($label));
        if (empty($clean)) $clean = substr(md5($label), 0, 8);
        return $clean;
    }
    
    function get_appointment_meta ($appointment_id) {
        $appointments_data = get_option('appointments_data', array());
        return empty($appointments_data[$appointment_id])
            ? array()
            : $appointments_data[$appointment_id]
            ;
    }
    
    function render_column_headers($table_headers) {
        global $appointments;
        $appointments->included_fields = array('Additional 3','Additional 2');
        $app_data = $appointments->options;
        $fields = !empty($app_data['additional_fields']) ? $app_data['additional_fields'] : array();
        if (empty($fields)) return $table_headers;
    
        foreach ($fields as $field) {
            $label = esc_html($field['label']);
            if(in_array($label, $appointments->included_fields)){
               $table_headers .= '<th class="all-appointments-status">' . __($label, 'appointments' ) . '</th>';
            }
        }
        return $table_headers;
    }
    
    add_filter('app_all_appointments_column_name', 'render_column_headers', 10, 1);
    
    function render_additional_cell($cell, $app) {
        global $appointments;
        $app_data = $appointments->options;
        $fields = !empty($app_data['additional_fields']) ? $app_data['additional_fields'] : array();
        if (empty($fields)) return $cell;
    
        $app_meta = get_appointment_meta($app->ID);
        if (empty($app_meta)) return $cell;
    
        foreach ($fields as $field) {
            $label = esc_html($field['label']);
            $name = to_clean_name($label);
            $value = !empty($app_meta[$name]) ? esc_attr($app_meta[$name]) : '';
            if(in_array($label, $appointments->included_fields)){
                $cell .= '<td>' . $value . '</td>';
            }
        }
        return $cell;
    }
    
    add_filter('app_all_appointments_add_cell', 'render_additional_cell', 10, 2);

    Note that you need to change the line
    $appointments->included_fields = array('Your Field Label here','Second field label');
    to include into the array a comma separated list with the label names of the fields that you want to render in the table.

    1. Can I change the heading names as it appears I get technical names (membershipnumber , membershipname)?

    In addition to what @Michael Bissett said above, remember that you can also use the translation file if you need to set the rendered name to something different to the label name.

    BTW: Is there any way to give points or anything on these pages to people like you who help ?

    You can give points to other regular members from the "Give Points" link. For Staff members like Mike or me, the best reward is to mark as resolved and upvote the thread. :wink:

    Please let us know if everything works as expected on your end.

    Cheers,
    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.