is_a_contact

📅 Published on

📝 Last updated

Documentation

»

Functions

»

is_a_contact

Returns true if the given value is an instance of \Groundhogg\Contact and the contact exists in the database. Use this as a guard before calling contact methods to avoid errors when a lookup might return false. Preferred over instanceof because it also confirms the record exists.

Parameters

ParameterTypeDescription
$contactmixedThe value to check.

Return value

bool — true if the value is an existing Contact object, false otherwise.

Usage

$contact = \Groundhogg\get_contactdata( $some_id );

if ( \Groundhogg\is_a_contact( $contact ) ) {
    $contact->apply_tag( 'verified' );
}

Example

Here’s a pseudo code example for a hypothetical integration that receives a contact from user input.

add_action( 'wp_ajax_my_plugin_process', 'my_plugin_process_contact_request' );

/**
 * Accept a contact ID or email from a request and process it safely.
 *
 * @return void
 */
function my_plugin_process_contact_request() {

    $id_or_email = sanitize_text_field( $_POST['contact'] ?? '' );
    $contact     = \Groundhogg\get_contactdata( $id_or_email );

    if ( ! \Groundhogg\is_a_contact( $contact ) ) {
        wp_send_json_error( 'Contact not found.' );
    }

    my_plugin_sync_to_external_crm( $contact );
    wp_send_json_success();
}

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.