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
| Parameter | Type | Description |
|---|---|---|
$user | WP_User|int|false | A WP_User object or user ID. Defaults to the currently logged-in user. |
$sync_meta | bool | When 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.
