Skip to content

Create Contact

corsacca edited this page Feb 21, 2018 · 14 revisions

Contact Creation (WIP)

This page is a work in progress. The code is being made to follow this documentation

The function source code: Create contact function

Parameters

create_contact( array $fields = [], $check_permissions = true )

  • fields (Required): an array of fields like name, phone number corresponding to the new contact. More details below.
  • check_permissions: check if the signed in user has the permission to perform this action. By default this should be true, unless this is called by an automated process.

Rest API:

  • url: https://example.com/wp-json/dt/v1/contact/create
  • type: POST
  • data: A JSON string of fields object. Required same format as defined below
  • contentType: "application/json; charset=UTF-8",
  • dataType: "json",

Returns

  • in $contact_id, the id of the newly created contact
  • WP_ERROR, in the case something went wrong.

Fields

Fields have different types. Each type will need it's own syntax. The list of fields will changed based on your install. For a list of available fields have a look at: Contact Post Type page

Text

Field examples:

  • title
fields = [
  "title" => "John Doe" 
]

key_select

Field examples:

  • overall_status
  • seeker_path

The options for each of these can be found in the Contact Post Type page The key is used to set save the field instead of the value.

fields = [
   "overall_status" => "established"
]

Contact Fields

  • phone_numbers
  • emails
  • addresses

The are separate from the text fields because there can be more than one of each. A contact can have several phone numbers. Once created each phone number will have it's own id.

$fields = [
  "phone_numbers" => [
    ["value" => "43 42 45 43"],
    ["value" => "94 39 29 39"]
  ] 
]

Date

  • baptism_date
$fields = [
  "baptism_date" => "2018-12-31" //format Y-m-d
]

Custom

  • assigned_to //int, a user id
  • subassigned //int, a contact id
$fields = [
   "assigned_to" => 4  //the id of the user
]

Number

  • quick_button_no_answer
  • quick_button_phone_off
  • quick_button_phone_off
  • quick_button_contact_established
  • quick_button_meeting_scheduled
  • quick_button_meeting_complete
  • quick_button_no_show
$fields = [
  "quick_button_no_answer" => 3
]

Connections

  • locations
  • groups
  • people_groups
  • baptized_by
  • baptized
  • coaching
  • coached_by
  • subassigned
$fields = [
  "locations" => [ "1", "9" ]
]

Fields example together

$fields = [
  "title" => "Bob",
  "overall_status" => "established",
  "phone" => [
    ["value" => "43 42 45 43"],
    ["value" => "94 39 29 39"]
  ],
  "locations" => [ "1", "9" ],
  "quick_button_no_answer" => 3,
  "baptism_date" => "2017-11-34",
]
Disciple_Tools_Contacts::create_contact( $fields, true )

JavaScript example with REST

jQuery.ajax({
  type: "POST",
  data: JSON.stringify({
    title:"bob", 
    phone_numbers:[{value:"12324"}]
  }),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  url: wpApiSettings.root + `dt/v1/contact/create`,
  beforeSend: function(xhr) {
    xhr.setRequestHeader('X-WP-Nonce', wpApiSettings.nonce);
  }                

Developer Documentation

Clone this wiki locally