create_contact_from_user

πŸ“… Published on

πŸ“ Last updated

Documentation

Β»

Functions

Β»

create_contact_from_user

Creates a Groundhogg contact record from an existing WordPress user. If the user already has a corresponding contact, the existing contact is returned rather than creating a duplicate. Optionally syncs user meta fields (first name, last name, etc.) to the contact record.

Parameters

ParameterTypeDescription
$userWP_User|int|falseWP_User object or user ID. Defaults to the currently logged-in user.
$sync_metaboolWhen true, syncs common user meta fields to the contact. Default false.

Return value

Contact|false β€” The created or existing Contact object, or false on failure.

Usage

$contact = \Groundhogg\create_contact_from_user( wp_get_current_user(), true );

if ( $contact ) {
    $contact->apply_tag( 'wp-user' );
}

Example

Here’s a pseudo code example for a hypothetical membership plugin integration.

add_action( 'my_membership_activated', 'my_plugin_sync_member_to_groundhogg' );

/**
 * Create a Groundhogg contact when a membership is activated.
 *
 * @param int $user_id
 *
 * @return void
 */
function my_plugin_sync_member_to_groundhogg( $user_id ) {

    $user    = get_userdata( $user_id );
    $contact = \Groundhogg\create_contact_from_user( $user, true );

    if ( ! $contact ) {
        return;
    }

    $contact->apply_tag( 'active-member' );
    \Groundhogg\after_form_submit_handler( $contact );
}

Was this helpful?

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