Creates a WordPress user account from a Groundhogg contact. The contact’s email is used as the username and a random password is generated. If a user with the same email already exists, that user is returned instead. The $notifications parameter controls whether WordPress sends its default new-user emails — pass false to suppress them.
Parameters
| Parameter | Type | Description |
|---|---|---|
$contact | Contact|int|string | A Contact object, contact ID, or email address. |
$role | string | The WordPress role to assign. Default 'subscriber'. |
$notifications | string|false | Whether to send new-user notifications. Accepts 'both', 'admin', 'user', or false. Default 'both'. |
Return value
int|false — The new (or existing) WordPress user ID, or false on failure.
Usage
$user_id = \Groundhogg\create_user_from_contact( $contact );
// With a specific role, no notifications
$user_id = \Groundhogg\create_user_from_contact( $contact, 'editor', false );
Example
Here’s a pseudo code example for a hypothetical course platform integration.
add_action( 'groundhogg/contact/tag_applied', 'my_plugin_create_student_account', 10, 2 );
/**
* Create a WordPress account when the "course-enrolled" tag is applied.
*
* @param \Groundhogg\Contact $contact
* @param int $tag_id
*
* @return void
*/
function my_plugin_create_student_account( $contact, $tag_id ) {
if ( $tag_id !== MY_PLUGIN_ENROLLED_TAG_ID ) {
return;
}
$user_id = \Groundhogg\create_user_from_contact( $contact, 'subscriber', false );
if ( ! $user_id ) {
return;
}
my_plugin_enrol_user_in_course( $user_id, MY_PLUGIN_DEFAULT_COURSE_ID );
// Send a custom welcome email instead of WP's default
\Groundhogg\send_email_notification( MY_PLUGIN_WELCOME_EMAIL_ID, $contact );
}
Was this helpful?
Let us know if this document answered your question. That’s the only way we can improve.
