Field Mapping

The field mapping API allows you to map fields between your product and ours very quickly and generate or update contact records without touching the DB.

Get the list of available field mappings.

To retrieve all the fields which can be mapped simply call the following function.

After of 2.0

<?php 
$fields = \Groundhogg\get_mappable_fields();

// Returns
// [
// 'first_name' => 'First Name',
// 'last_name' => 'Last Name',
// 'email' => 'Email',
// ...
// ]
?>

Before 2.0

<?php 
$fields = wpgh_get_mappable_fields();
?>

This will return a list of key => name pairs where the key is the field in Groundhogg and the value is the pretty name of the field, this makes it easy to use these keys in a select element.

Generate contacts with a mapping.

You will want to generate or update contacts using the field mapping API for a variety of reasons to avoid unneeded code and extraneous time spent debugging.

Let’s say you have an array of data which the keys of which do not match up to any existing fields in Groundhogg.

<?php 

$data = [
  'fname' => 'John',
  'lname' => 'Doe',
  'email_address' => 'john.doe@gmail.com'
  'birthday' => '1997'
  'gender' => 'male'
];

?>

None of the above fields exist anywhere in Groundhogg, but we can map them to fields that do exist.

<?php 

$map = [
  'fname' => 'first_name',
  'lname' => 'last_name',
  'email' => 'email',
  'birthday' => 'meta'
  'gender' => 'tags'
];

?>

Above we mapped the generic details to the database fields we know exist, and we have also mapped birthday to the metadata and mapped their gender to a tag to be applied!

Now we actually have to generate our contact.

As of 2.0

<?php 

$data = [
  'fname' => 'John',
  'lname' => 'Doe',
  'email_address' => 'john.doe@gmail.com'
  'birthday' => '1997'
  'gender' => 'male'
];

$map = [
  'fname' => 'first_name',
  'lname' => 'last_name',
  'email' => 'email',
  'birthday' => 'meta'
  'gender' => 'tags'
];

$contact = \Groundhogg\generate_contact_with_map( $data, $map );

if ( ! $contact ){
  return false;
}

?>

Before 2.0

<?php 

$data = [
  'fname' => 'John',
  'lname' => 'Doe',
  'email_address' => 'john.doe@gmail.com'
  'birthday' => '1997'
  'gender' => 'male'
];

$map = [
  'fname' => 'first_name',
  'lname' => 'last_name',
  'email' => 'email',
  'birthday' => 'meta'
  'gender' => 'tags'
];

$contact = wpgh_generate_contact_with_map( $data, $map );

if ( ! $contact ){
  return false;
}

?>

Articles

Was this article helpful to you? Yes No

How can we help?