Contacts

Groundhogg provides public methods to directly communicate with the contacts database. There are various methods available in Groundhogg using which  you can perform database operations and manage your contacts using simple PHP code.

add()

Add method use to add new contact inside database. this function accepts array of records and update value in database. it returns the id of a contact which is inserted by the add operation. this operation requires email address as an argument to perform operation successfully.

Example: This code creates new contact in database.

$data = array (
    'first_name'   => 'John',
    'last_name'    => 'Doe',
    'email'        => 'email@example.com'
    'user_id'      => 1,
    'owner_id'     => 1,
    'optin_status' => WPGH_CONFIRMED,
);
$contact_id = WPGH()->contacts->add ( $data );
echo 'contact id is  : ' . $contact_id ; 

update()

This operation update details of existing contact with new details. this operation  accepts contact id and array of updated value as parameter and returns true or false based on the operation status.

Example  : This code updates first and last name of contact.

$data = array (
    'first_name' => 'John',
    'last_name'  => 'Doe'             
);

$contact_id = 10;

$result = WPGH()->contacts->update ( $contact_id, $data );

if ( $result == true ) {
    echo 'Contact updated successfully'; 
} else {
    echo 'Contact not updated';
}

delete()

This function used to remove contact from database. you can delete contact using email address of contact or using contact id. this operation returns true or false based on the result of operation.

$contact_id  = 10 ;
$result      = WPGH()->contacts->delete ( $contact_id );

if ( $result == true ) {
    echo 'Contact deleted successfully'; 
} else {
    echo 'Contact not deleted';
}

get_contacts()

This function search data base and fetch relative contact from the database. This method accepts array as an argument and returns array of contact details if found in database and returns null if contact not found in database.

Example : This code fetch contact from the database which email address is ’email@example.com’

$result = WPGH()->contacts->get_contacts ( array( 'email' => 'email@example.com' ) );   

if ( empty( $result ) ) {
    echo 'sorry no contacts found with this email address'; 
} else {
    print_r( $result );
}

get_contact_by()

This function used to find contact using contact id or email address. this function accepts email or id as a parameter. This operation returns object of contact if operation performed successfully and returns null if contact not found in database.

$result = WPGH()->contacts->get_contact_by( 'ID', 10 );   

if ( $result == null ) {
    echo 'Sorry, no contact found with this id'; 
} else {
    echo 'Contact's First Name is  : ' . $result->first_name; 
}

count()

This function used to find number of contacts match with the search criteria. This function accepts array as an argument and returns integer number based on the match in the database.

Example : This code fetch counts number of contacts who’s first name is ‘Jhon’

$result = WPGH()->contacts->count( array( 'first_name' => 'john' ) ); 

echo 'We found : ' . $result . ' result with your search criteria';

exists()

This function used to check if the some spacial record founds in a database or not. This function accepts value to search for and field name as an argument. It return true or false based on the result of an operation.

Example : This code try to search for an email address with value ’email@example.com’ .

$result = WPGH()->contacts->exists ( 'email@example.com' , 'email' );

if ( $result == true ) {
    echo 'email found'; 
} else {
    echo 'email not found';
}
Was this article helpful to you? Yes No

How can we help?