Passing meta_data from UPME custom fields to Appointments+ Custom fields

I am using UPME with Members plugin for a role based registration and management. Can I pass data from UPME fields to the Appointments+ confirmation form when subscribers start filling in their data. UPME uses standard user_meta data. I need help passing that information to the custom fields. If I create custom fields in Appointments+ for some of the same fields I use with UPME, does it have separate meta keys or can I pass the data from User Registration using standard meta_keys. I need help accomplishing this. Please help.

    • Paul Kevin

      Hello @Venkatesh Krishnamurthy,

      How are you today?

      You can activate the add-on "Additional Fields" to add extra fields needed. Appointments uses the WordPress user meta data and a custom database table to store the appointments.

      If you wish to add extra text after the "Google Calender" button, you can use this :

      add_filter( 'app_additional_fields', 'some_cool_function', 10, 1);

      with the function

      function some_cool_funtion($ret){
      	return $ret."this is my text";
      }

      If you need further assistance, we are here to help.

      Thank you

  • Venkatesh Krishnamurthy

    I have actually activated the Additional Fields Addon in Appointments+ setting. What I am trying to do, is pass custom field data to these additional fields. Your instructions dont help since the filters have been inserted by the Addon already.

    Adding the addon fields doesnt ensure that if such a field exists, it captures the user_meta from it. Besides my previous question of if the new addon fields in Appointments+ creates a new meta_value for an existing field has not been answered.

    I am not sure how to pass the user_meta to the additional fields.

    For e.g. I have created two fields 1. Medicare No and 2. Referring practitioner with meta values of medicare_no and gp_name

    I need to pass the data from the user_meta table to these 2 additional fields that have already been inserted after the GCal button (perhaps the developers need to modify the template here... i had to amend it so that the additional fields appear before the GCal button and checkbox rather than after that - a simple solution otherwise the form looks crazy). The trouble is I can't declare them since it uses a filter that I haven't defined. I looked in the addons folder in the plugins directory for Appointments+ and found this app-users-additiona_fields.php file. I am not sure if this is the file where I can amend code to get the user_meta for the two custom fields to be passed. I am not a developer and don't even have an IT background but I have some knowledge of changing code but need some guidance here on how to do it. Could you please give me a better explanation of how I can accomplish this and then I can expand it to multiple additional fields?

    Many thanks

    • Paul Kevin

      Hello @Venkatesh Krishnamurthy,

      If you check the file in the directory appointments/includes/app_shortcodes.php from line 1637 you will see the section where the user_meta is collected and populated to the form. To pass your custom meta data, first use this code after line 1667:

      $medicare_no= get_user_meta( $current_user->ID, 'medicare_no', true );
      $gp_name= get_user_meta( $current_user->ID, 'gp_name', true );

      Then add this code before line 1703 :

      $ret .= '<div class="appointments-medicare-field" style="display:none">';
      $ret .= '<label><span>'. __("Medicare Number","appointments") . '</span><input type="text" class="appointments-medicare-field-entry" id="' . esc_attr(apply_filters('app-shortcode-confirmation-medicare_no_id', 'appointments-field-medicare_no')) . '" value="'.$medicare_no.'" /></label>';
      $ret .= '</div>';
      $ret .= '<div class="appointments-gp-field" style="display:none">';
      $ret .= '<label><span>'. __("GP Name","appointments") . '</span><input type="text" class="appointments-gp-field-entry" id="' . esc_attr(apply_filters('app-shortcode-confirmation-gp_name_id', 'appointments-field-gp_name')) . '" value="'.$gp_name.'" /></label>';
      $ret .= '</div>';

      Hope that helps. If you need further assistance, we re here to help :slight_smile:

  • Venkatesh Krishnamurthy

    Hi Paul,

    I have managed to get it working but it needs further tweaking.

    I have 2 additional custom phone fields and by changing the following $phone_meta code to
    $phone_meta = get_user_meta( $current_user->ID, 'app_phone', true );

    if ( $phone_meta )
    $p = $phone_meta;
    else if ( $user_info->mobile_phone )
    $p = $user_info->home_phone;
    else if ( $user_info->mobile_phone )
    $p = $user_info->home_phone;
    else if ( $user_info->work_phone )
    $p = $user_info->work_phone;

    I have been able to load the first preferred phone number to show up in the fields.

    I declared the additional fields as suggested with an extra step just to match the code -

    I added just a single field (Medicare #) as

    $medicare_meta = get_user_meta( $current_user->ID, 'medicare_no', true );
    $m = $medicare_meta;

    I have added the below code to display this field in the confirmation area and it works fine.

    $ret .= '<div class="appointments-phone-field" style="display:none">';

    $ret .= '<label><span>Medicare No</span><input type="text" class="appointments-medicare-field-entry" id="' . esc_attr(apply_filters('app-shortcode-confirmation-medicare_field_id', 'appointments-field-customer_medicare')) . '" value="'.$m.'" /></label>';

    $ret .= '</div>';

    The main problem is:

    1. The Medicare Field displays and the value is shown - but this is fairly useless. The field doesn't show up in the backend and ofcourse if it's empty or incorrect, although the user can change it, nothing happens because there is no code to tell the form to update the Meta value. I need this to happen so that if the user finds that this value is missing and/or needs correcting then the field value is updated. I am not sure where to pass the arguments to get the form to update the user_meta if a client changes the value.

    2. How can i get the field to show up in the backend?

    3. How can I then have a field validation to force user to complete it and not leave it empty.

    If I can understand the code structure, I can get somewhere but it is tricky not knowing what files do what.

    I would be most grateful if you can help me accomplish this.

    Hopefully someone will find a way to create a plugin to make this an easy job for all non coding people.

  • Venkatesh Krishnamurthy

    Hi Paul,

    It's fantastic. It works and I can see the custom fields in the backend with the data too. There was one thing I tried which didn't work. I had already filled in the value for the Medicare_no when I edited my Profile. I tried to see if I change the value, if it updates the meta table with the updated value. It doesn't work. Any chance of ensuring that if the value already exits and if the value is changed, then the meta table is updated too with the new value?

    Many thanks for creating this custom code. It makes Appointments+ fantastic. This was the last bit that was missing for me.

  • Venkatesh Krishnamurthy

    Hi Paul,

    I hadn't deleted the older field that i had inserted in the app_class_shortcodes.php. I might have amended the value in the older form field. I will remove that code and check before you have to troubleshoot. I will let you know. It looks like the code to update user_meta already exists. I think it might have been the duplicate field at my end.

    I will let you know soon.

  • Venkatesh Krishnamurthy

    Hi Paul,

    I have deleted the extra field I had created before
    Now as far as the medicare_no field is concerned, this is what I have observed.

    If I have a value for this field, it displays when the confirmation form loads but it cannot be updated. If I change the value, it doesn't update it. Also the value that shows up on the backend is not editable.

    If there is no value set and I have created a custom field, it does not insert the value and it does not show up in the backend. It only shows up in the backend if the custom field was already defined, a meta exists and a value has been set.

    I created a new custom field which I had not defined before and it showed up in the front-end but when I go to the backend, it only shows the medicare_no field but not the new Custom field in the back end. Strangely, when I tried to create the appointment, the Medicare No field did not actually show in the front end.

    I will update the fix and re test it.
    I have created an admin account if you wanted to have a look. Let me know how I can send you a private message with the Wordpress admin details.

    Cheers,

    • Paul Kevin

      Hello @Venkatesh Krishnamurthy,

      I have received the credentials :slight_smile: Managed to fix the loop issue and I have updated the plugin on your server :slight_smile: with the code below :

      <?php
      /*
      Plugin Name: Extended Appointments
      Plugin URI: https://premium.wpmudev.org/
      Description: Adds custom fields to the appointments plugin and allows you to set the user meta data name
      Author: Paul Kevin
      Version: 1.0
      */
      
      if(!class_exists('WPMU_Extended_Appointments')) {
      
      	/**
      	 * Loads the Extended Appointment plugin.
      	 *
      	 * Load the Extended Appointment plugin for WPMU Appointments plugin
      	 *
      	 * @since 1.0
      	 */
      	class WPMU_Extended_Appointments{
      
      		/**
      		 * Plugin Directory of the site
      		 *
      		 * @since 1.0
      		 *
      		 * @var string
      		 */
      		public $plugin_directory = '';
      
      		/**
      		 * Plugin URL of the site
      		 *
      		 * @since 1.0
      		 *
      		 * @var string
      		 */
      		public $plugin_url = '';
      
      		/**
      		 * Configures the plugin and future actions.
      		 *
      		 * @since 1.0
      		 */
      		public function __construct() {
      
      			// set plugin directory to load plugin files
      			$this->plugin_directory = WP_PLUGIN_DIR.'/'.dirname(plugin_basename(__FILE__));
      
      			// set plugin url to reference files in the plugin directory
      			$this->plugin_url = WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__));
      
      			//Create and register Admin menu
      			add_action("admin_menu", array(&$this, 'admin_menu' ));
      
      			//Set the filter
      			add_filter( 'app_additional_fields', array(&$this, 'user_fields'), 10, 1);
      
      			add_action('app_new_appointment', array(&$this, 'save_submitted_fields'));
      
      			add_filter('app-footer_scripts-after', array($this, 'inject_additional_fields_script'));
      
      			add_action('app-additional_fields-validate', array($this, 'validate_submitted_fields'));
      
      			// Display additional notes
      			add_filter('app-appointments_list-edit-client', array($this, 'display_inline_data'), 10, 2);
      		}
      
      		/**
      		 * Initialize the backend, administrative views.
      		 *
      		 * @since 1.0
      		 *
      		 * @return void
      		 */
      		function admin_menu(){
      			add_submenu_page('appointments', __('Custom Fields','appointments'), __('Custom Fields','appointments'), App_Roles::get_capability('manage_options', App_Roles::CTX_PAGE_SETTINGS), "app_extended_settings", array(&$this,'settings'));
      		}
      
      		/**
      		 * Create the settings page
      		 *
      		 * @since 1.0
      		 *
      		 * @return void
      		 */
      		function settings(){
      			// Use nonce for verification
      			wp_nonce_field( plugin_basename( __FILE__ ), 'ext_appointment_noncename' );
      			if (! empty( $_POST ) && check_admin_referer('ext_appointment_settings','ext_appointment_noncename') ){
      				$total = 20;
      				$form_elem = array();
      				while($total > 0){
      					if(!empty($_POST['name_'.$total])){
      						$form_elem[$total]['name'] = sanitize_text_field($_POST['name_'.$total]);
      						$form_elem[$total]['type'] = sanitize_text_field($_POST['type_'.$total]);
      						$form_elem[$total]['umeta'] = sanitize_text_field($_POST['u_meta_'.$total]);
      						$form_elem[$total]['mandatory'] = (empty($_POST['manadatory_'.$total])) ? 'notchecked' : 'checked';
      					}
      					$total--;
      				}
      
      				update_option('wpmu_ext_appointment_settings', $form_elem);
      
      			}
      
      			$wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
      			?>
      			<div class="wrap">
      				<form action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" method="post">
      					<?php wp_nonce_field('ext_appointment_settings','ext_appointment_noncename'); ?>
      					<p class="submit">
      						<input class='button-primary' type='submit' value='<?php _e('Save Options','appointments'); ?>'/><br/>
      					</p>
      					<table width="100%" border="0" class="widefat">
      						<thead>
      							<tr>
      								<th width="40%" align="left" scope="col"><?php _e('Name','appointments'); ?></th>
      								<th width="10%" align="left" scope="col"><?php _e('Type','appointments'); ?></th>
      								<th width="40%" align="left" scope="col"><?php _e('User Meta Key','appointments'); ?></th>
      								<th width="10%" align="left" scope="col"><?php _e('Mandatory','appointments'); ?></th>
      							</tr>
      						</thead>
      						<tfoot>
      							<tr>
      								<th width="40%" align="left" scope="col"><?php _e('Name','appointments'); ?></th>
      								<th width="10%" align="left" scope="col"><?php _e('Type','appointments'); ?></th>
      								<th width="40%" align="left" scope="col"><?php _e('User Meta Key','appointments'); ?></th>
      								<th width="10%" align="left" scope="col"><?php _e('Mandatory','appointments'); ?></th>
      							</tr>
      						</tfoot>
      						<tbody>
      							<?php
      								$total = 20;
      								while($total > 0){
      
      									?>
      									<tr>
      										<td><input type="text" name="name_<?php echo $total; ?>" value="<?php echo $wpmu_ext_appointment_settings[$total]['name']; ?>"/></td>
      										<td>
      											<select name="type_<?php echo $total; ?>">
      												<option value="1" <?php selected($wpmu_ext_appointment_settings[$total]['type'], 1); ?> ><?php _e('Text Field','appointments'); ?></option>
      												<option value="2"<?php selected($wpmu_ext_appointment_settings[$total]['type'], 2); ?> ><?php _e('Check Box','appointments'); ?></option>
      
      											</select>
      										</td>
      										<td><input type="text" name="u_meta_<?php echo $total; ?>" value="<?php echo $wpmu_ext_appointment_settings[$total]['umeta']; ?>" /></td>
      										<td><input type="checkbox" value="checked" name="manadatory_<?php echo $total; ?>" <?php echo ($wpmu_ext_appointment_settings[$total]['mandatory'] == 'checked') ? "checked='checked'": ""; ?> /></td>
      									</tr>
      									<?php
      									$total --;
      								}
      							?>
      						</tbody>
      					</table>
      					<p class="submit">
      						<input class='button-primary' type='submit' value='<?php _e('Save Options','appointments'); ?>'/><br/>
      					</p>
      				</form>
      			</div>
      			<?php
      		}
      
      		/**
      		 * Show the user fields on the form
      		 *
      		 * @since 1.0
      		 *
      		 * @param String $ret
      		 *
      		 * @return String
      		 */
      		function user_fields($ret){
      			$wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
      			if (is_array($wpmu_ext_appointment_settings) && count($wpmu_ext_appointment_settings) > 0) {
      				$loggedin = is_user_logged_in();
      				if($loggedin){
      					global $current_user;
      				}
      				$pos = 0;
      				foreach($wpmu_ext_appointment_settings as $wpmu_ext_appointment_setting){
      					$input_name = $wpmu_ext_appointment_setting['name'];
      					if(!empty($input_name)){
      						$input_type = $wpmu_ext_appointment_setting['type'];
      						$meta_key = $wpmu_ext_appointment_setting['umeta'];
      						$meta_value = "";
      						$input_value = "";
      						if($loggedin){
      							$meta_value = get_user_meta( $current_user->ID, $meta_key, true );
      						}
      						$form_class = '';
      						if($wpmu_ext_appointment_setting['mandatory'] == 'checked'){
      							$form_class = 'required="required"';
      						}
      						if($input_type === '1'){
      							$input_value = '<input type="text" class="appointments-custom-field appointment-'.$meta_key.'" name="appointment-'.$meta_key.'" value="'.$meta_value.'" id="appointment-'.$meta_key.'" '.$form_class.' />';
      						}else if($input_type === '2'){
      							$input_value = '<input type="checkbox" class="appointments-custom-field appointment-'.$meta_key.'" name="appointment-'.$meta_key.'" id="appointment-'.$meta_key.'" '.$form_class.' />';
      						}
      						$ret .= '<div class="appointments-phone-field" style="display:none">';
      						$ret .= '<label><span>'.__($input_name,'appointments').'</span>'.$input_value.'</label>';
      						$ret .= '</div>';
      					}
      				}
      			}
      			return $ret;
      		}
      
      		/**
      		 * Inject the values to the post script
      		 *
      		 * @since 1.0
      		 *
      		 */
      		public function inject_additional_fields_script () {
      			?>
      				<script type="text/javascript">
      				(function ($) {
      				$(document).ajaxSend(function(e, xhr, opts) {
      					if (!opts.data) return true;
      					if (!opts.data.match(/action=post_confirmation/)) return true;
      
      					var $fields = $(".appointments-custom-field");
      					$fields.each(function () {
      						var $me = $(this),
      							name = $me.attr("name"),
      							value = $me.is(":checkbox") ? ($me.is(":checked") ? 1 : 0) : $me.val()
      						;
      						opts.data += '&' + encodeURIComponent(name) + '=' + encodeURIComponent(value);
      					});
      				});
      				})(jQuery);
      				</script>
      			<?php
      		}
      
      		/**
      		 * Validate the submitted fields
      		 *
      		 * @since 1.0
      		 */
      		public function validate_submitted_fields () {
      			$wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
      			if (empty($wpmu_ext_appointment_settings)) return false;
      
      			foreach($_POST as $key => $value) {
      				if (strpos($key, 'appointment-') === 0) {
      					if(empty($key)){
      						die( json_encode( array("error"=>sprintf( __( 'Something wrong about the submitted %s', 'appointments'), ""))));
      					}else{
      						continue;
      					}
      				}
      			}
      		}
      
      		/**
      		 * Save the submitted fields
      		 *
      		 * @since 1.0
      		 */
      		function save_submitted_fields($appointment_id){
      			$wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
      			if ( is_user_logged_in()) {
      				global $current_user;
      				$raw = stripslashes_deep($_POST);
      				$duration = strlen("appointment-");
      				foreach($raw as $key => $value) {
      					if (strpos($key, 'appointment-') === 0) {
      						$meta_key =  substr("appointment-medicare_no", $duration);
      						if(!empty($meta_key)){
      							if($value)
      								update_user_meta( $current_user->ID, $meta_key, $value );
      						}
      					}
      				}
      
      			}
      
      		}
      
      		public function display_inline_data ($form, $app) {
      			$wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
      			if (empty($wpmu_ext_appointment_settings)) return $form;
      			$total = count($wpmu_ext_appointment_settings);
      			$user = get_user_by( 'email', $app->email );
      			if($user){
      				foreach($wpmu_ext_appointment_settings as $wpmu_ext_appointment_setting){
      					$input_name = $wpmu_ext_appointment_setting['name'];
      					if(!empty($input_name)){
      						$input_type = $wpmu_ext_appointment_setting['type'];
      						$meta_key = $wpmu_ext_appointment_setting['umeta'];
      						$meta_value = get_user_meta( $user->ID, $meta_key, true );
      						if($meta_value){
      							$form .= '<label>' .
      								"<span class='title'>{$input_name}</span>" .
      								"<span class='input-text-wrap'><input type='text' class='widefat' disabled='disabled' value='{$meta_value}' /></span>" .
      							'</label>';
      						}
      					}
      				}
      			}
      			return $form;
      		}
      
      	}
      }
      
      /**
       * Load plugin function during the WordPress init action
       *
       * @since 1.0
       *
       * @return void
       */
      function wpmu_extended_appointments() {
      	global $wpmu_extended_appointments;
      
      	$wpmu_extended_appointments = new WPMU_Extended_Appointments();
      }
      add_action( 'init', 'wpmu_extended_appointments', 0 ); // load before widgets_init at 1
      ?>

      Thanks

  • Venkatesh Krishnamurthy

    Hi Paul,

    There is something strange still going on.
    It works if I have a single custom field but with more than one field, it doesn't work and it inserts the values in the wrong place.

    I just noticed in the plugin where it submits data :
    * Save the submitted fields
    *
    * @since 1.0
    */
    function save_submitted_fields($appointment_id){
    $wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
    if ( is_user_logged_in()) {
    global $current_user;
    $raw = stripslashes_deep($_POST);
    $duration = strlen("appointment-");
    foreach($raw as $key => $value) {
    if (strpos($key, 'appointment-') === 0) {
    $meta_key = substr("appointment-medicare_no", $duration);
    if(!empty($meta_key)){
    if($value)
    update_user_meta( $current_user->ID, $meta_key, $value );
    }
    }
    }

    }

    }

    I am not sure what the following line is meant to do -
    $meta_key = substr("appointment-medicare_no", $duration);

    but it appears that it restricts the data submission to the medicare_no field. If i remove that from the custom field and input data using 2 additional new custom fields, then the first field value still inserts into the Medicare No and when I actually check my profile where the key exists, the medicare no does change.

    For eg.
    If I have 3 custom fields with 1. Medicare No 2. Referring practitioner and 3. GP Surgery and I submit an appointment request, the backend only shows input for Medicare No. If I remove that from custom field and leave the remaining two, then the first value for Referring Practitioner gets inserted but into the Medicare no field.

    Do you need to define the form submission as an array (please ignore my stupidity - I have absolutely no coding experience) so that all sequential fields that are declared by the Custom Field insert, is actually inserted into the User Table?

    Looks like we are nearly there - looking forward to your help again.

    Many thanks,

    • Paul Kevin

      Ive fixed the function, that was code left from a test :slight_smile: Kindly try now

      /**
      		 * Save the submitted fields
      		 *
      		 * @since 1.0
      		 */
      		function save_submitted_fields($appointment_id){
      			$wpmu_ext_appointment_settings = get_option('wpmu_ext_appointment_settings');
      			if ( is_user_logged_in()) {
      				global $current_user;
      				$raw = stripslashes_deep($_POST);
      				$duration = strlen("appointment-");
      				foreach($raw as $key => $value) {
      					if (strpos($key, 'appointment-') === 0) {
      						$meta_key =  substr($key, $duration);
      						if(!empty($meta_key)){
      							if($value)
      								update_user_meta( $current_user->ID, $meta_key, $value );
      						}
      					}
      				}
      
      			}
      
      		}
  • Venkatesh Krishnamurthy

    Hi Paul,

    Thanks very much for that. It works like a charm. You are awesome.
    Could I ask one last favour?

    Any chance of editing the Custom fields in the backend? They seem to be locked. Which file should I edit to make them editable? Perhaps the same update_user_meta function works there too.

    I just wanted to decrease the width of the text fields and increase the column width for the field labels in the backend. My Referring practitioner is split up and looks messy. What code should I edit to get it right?

    I am sure there are a lot of other people out there who were exactly looking for the same function. They will all find your plugin very helpful.

    Many thanks again,
    Cheers,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.