wpf_format_field_value

#Overview

Most of WP Fusion’s supported CRMs support different custom field types in addition to text fields, for example dates, picklists, dropdowns, and radios.

The wpf_format_field_value filter is run on each field value before it’s synced to your CRM, to convert WordPress formatted data into the appropriate format in your CRM.

#Parameters

  • $value: (mixed) The field value that will be synced to the CRM
  • $field_type(string) The field type as shown on the Contact Fields list in the WP Fusion settings
  • $field: (string) The field’s API name in your CRM

#How it works in WP Fusion

This happens in two places, at two priorities. In the class WPF_CRM_Base there is a format_field_value() function attached to priority 5 that does some standardized formatting for all CRMs:

  • Dates are converted into Unix timestamps
  • Array values (like multiselects) are combined into a comma-separated string
  • Checkbox values are converted to 1 if they are checked or null if they are un-checked.

Then in each CRM integration module there is a format_field_value() function attached to priority 10, which does additional formatting based on that CRM’s API. For example:

  • With ActiveCampaign, multiselect values are separated with pipes || to properly update multiselect fields
  • With Infusionsoft dates are converted from their UNIX timestamps into ISO 8601 dates
  • With Mautic states and countries are converted into Mautic-compatible state and country abbreviation codes

#Examples

You may sometimes need to change the way field values are formatted before being synced to your CRM in addition to WP Fusion’s default formatting. To use the code examples below, add them to your active theme’s functions.php file.

#Convert Yes / No strings to checkbox values

This example converts “Yes” and “No” values submitted on a form to boolean values, for updating checkbox-type fields in your CRM.

function custom_format_checkbox( $value, $field_type, $field ) {

	if ( 'Yes' == $value ) {
		$value = true;
	} elseif ( 'No' == $value ) {
		$value = null;
	}

	return $value;

}

add_filter( 'wpf_format_field_value', 'custom_format_checkbox', 20, 3 );

#Convert checkbox values to strings

WP Fusion syncs checkbox values to your CRM as boolean true and null, which is the most compatible with updating checkbox and radio type custom fields in your CRM.

However, you may want to see the actual text “True” and “False” in your CRM. This example converts those boolean values back to strings.

function custom_format_checkbox( $value, $field_type, $field ) {

	if ( 'checkbox' == $field_type && true == $value ) {
		$value = 'True';
	} elseif ( 'checkbox' == $field_type && null == $value ) {
		$value = 'False';
	}

	return $value;

}

add_filter( 'wpf_format_field_value', 'custom_format_checkbox', 20, 3 );

#Prepend a 1 to phone numbers

If your CRM has a phone-type field that enforces a certain length, you may need to prepend a country code to the number for it to sync properly.

function custom_format_phone( $value, $field_type, $field ) {

	if ( 'phone' == $field && strlen( (string) $value ) <= 10 ) {

		$value = '1' . $value;

	}

	return $value;

}

add_filter( 'wpf_format_field_value', 'custom_format_phone', 20, 3 );

#Custom formatting for dates

You may want to store a date in a text field in a different format from the default for your CRM.

function custom_format_date( $value, $field_type, $field ) {

	if ( $field_type == 'datepicker' || $field_type == 'date' ) {

		$value = date( 'Y-m-d', strtotime( $value ) );

	}

	return $value;

}

add_filter( 'wpf_format_field_value', 'custom_format_date', 20, 3 );

#Force an empty value

By default WP Fusion won’t sync an empty text value to your CRM, to avoid overwriting existing data. You can override this by setting the field’s value to null.

In this example we’ll set WP Fusion to always send a null value any time data isn’t specified for the CRM field with key mycrmfieldkey:

function custom_format_empty_fields( $value, $field_type, $field ) {

	if ( 'mycrmfieldkey' == $field && empty( $value ) ) {

		$value = null;

	}

	return $value;

}

add_filter( 'wpf_format_field_value', 'custom_format_empty_fields', 4, 3 );

Or, to target based on field type:

function custom_format_empty_dates( $value, $field_type, $field ) {

	if ( 'date' === $field_type && empty( $value ) ) {

		$value = null;

	}

	return $value;

}

add_filter( 'wpf_format_field_value', 'custom_format_empty_dates', 4, 3 );

Note that the priority on the filters is 4, so it runs before WP Fusion has done any other formatting on the field value.

Was this helpful?