pull_user_meta()

pull_user_meta()

#Overview
This function triggers WP Fusion to load data from your CRM back to the WordPress user record. Any fields enabled for sync on the Contact Fields tab will be loaded.
#Parameters

$user_id: the WordPress user ID to load the metadata for

#Returns

$user_meta: array of key / value pairs of WordPress meta

#Examples
#Load a number and increment it
This example loads a custom field from the CRM, adds 1 to it if it』s numeric, and syncs the field value back to the CRM.
$user_meta = wp_fusion()->user->pull_user_meta( $user_id );

if ( ! empty( $user_meta['number_field'] ) && is_numeric( $user_meta['number_field'] ) ) {

$user_meta['number_field']++;

wp_fusion()->user->push_user_meta( $user_id, array( 'number_field' => $user_meta['number_field'] ) );

}

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

push_user_meta()

push_user_meta()

#Overview
This function allows you to sync WordPress user metadata to fields in your CRM.
#Sending specific data
You can send specific fields by providing an array of field names and values.
$update_data = array(
'first_name' => 'Joe'
'custom_field' => 'Custom Value'
)

wp_fusion()->user->push_user_meta( $user_id, $update_data );
The field keys should be WordPress meta fields keys. WP Fusion will use the field mapping you』ve set on the Contact Fields tab to associate the data with the corresponding fields in your CRM.
#Sending all user data
You can also omit the array of update data, and WP Fusion will get all of the information it can find about the user from the database and send it to your CRM.
wp_fusion()->user->push_user_meta( $user_id );
 

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

remove_tags()

remove_tags()

#Overview
This function allows you to remove an array of tags (using tag IDs) from a user.
#Remove tags from the current user
$tags = array(123, 456, 789);
wp_fusion()->user->remove_tags( $tags );
#Remove tags from a specific user ID
$tags = array(123, 456, 789);
wp_fusion()->user->remove_tags( $tags, $user_id );
The function will return true if the tags were removed successfully, and false if there was a connection error or the user wasn』t found in the CRM. Any errors will be logged to the WP Fusion Logs.
#Examples
#Remove a tag when another tag is applied
WP Fusion includes a lot of interfaces for applying tags, but because removing tags is less common most of our integrations don』t include options for removing tags.
This example removes the tag Pending Signup when the tag Profile Complete is applied.
function remove_pending_signup_tag( $user_id, $tags_applied ) {

$tag_to_check = wpf_get_tag_id( 'Profile Complete' );
$tag_to_remove = wpf_get_tag_id( 'Pending Signup' );

if ( in_array( $tag_to_check, $tags_applied ) ) {
wp_fusion()->user->remove_tags( array( $tag_to_remove ), $user_id );
}

}

add_action( 'wpf_tags_applied', 'remove_pending_signup_tag', 10, 2 );

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

user_can_access()

user_can_access()

#Overview
This function determines whether the a user can access a given post (by ID). It can use the current logged in user, as in the example below, or an alternate user ID can be provided.
#Basic Usage
The basic usage takes a post ID and tells you whether or not the current user has access to that post, based on the access rules configured in the WP Fusion meta box, and the current user』s logged-in status and CRM tags.
if ( wpf_user_can_access( $post_id ) ) {

echo "Welcome to the post.";

} else {

echo "Sorry, but this content is restricted.";

}

#With a user ID
You can also pass in a user ID as the second argument to perform the check on other users. For example, to check by a user』s email address:
$user = get_user_by( 'email', '[email protected]' );

if ( wpf_user_can_access( $post_id, $user->ID ) ) {

echo "Welcome to the post.";

}
#Within a loop
When used within a loop, you can omit the $post_id and WP Fusion will check the access rules against the current post:
if ( wpf_user_can_access() ) {

echo "Welcome to the post.";

}

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

wpf_get_current_user()

wpf_get_current_user()

#Overview
This function works similarly to the wp_get_current_user function, but it will also work during an auto-login session.
The function will return a WP_User object, or false if the user isn』t known.
#Get the current user
$user = wpf_get_current_user();
if ( false !== $user ) {
echo $user->user_email; // The email address of the current user, or temporary auto-login user
}

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

get_users_with_tag()

get_users_with_tag()

#Overview
This function returns an array of user IDs who have a specified tag. You can use either a tag ID or label.
#Get all users with the Member tag
$user_ids = wpf_get_users_with_tag( 'Member' );
#Get all users with tag ID 123
$user_ids = wpf_get_users_with_tag( 123 );

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

has_tag()

has_tag()

#Overview
This function determines whether a user has a given tag. A tag ID or label can be provided.
if ( wpf_has_tag( 'Paying Customer' ) ) {

echo "Thanks for your payment!";

}

Or more complex conditions can be created by combining calls to has_tag(), for example:
if ( wpf_has_tag( 'New Customer' ) && ! wpf_has_tag( 'Watched Intro Video' ) ) {

echo "Welcome to our site! Please watch our introduction video here:";

}

#By User ID
To check the tags for a specific user, pass a $user_id as the second parameter:
if ( wpf_has_tag( 'Paying Customer', $user_id ) ) {

echo "Thanks for your payment!";

}

#Array Syntax
The function also accepts an array of tag names or IDs. If the user has any of the provided tags the function will return true.
if ( wpf_has_tag( array( 'Pending Affiliate', 'Accepted Affiliate' ) ) {

echo "Thanks for joining our affiliate program!";

}

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

add_object()

add_object()

#Overview
This helper function is available with Ontraport, Zoho, Salesforce, HubSpot, and other CRMs which use custom objects.
It』s a shortcut / alternative to using the wpf_crm_object_type filter, for cases where you just need to add a custom object one time, without changing the object type used for WP Fusion globally.
#Parameters

$data: (array) An associative array of data to sync to the CRM, using CRM field IDs for the keys
$object_type: (string) The object type you wish to update

#Examples
#Create a new Lead in Salesforce
$data = array(
'Email' => '[email protected]',
'FirstName' => 'Jane',
'LastName' => 'Doe',
);

$lead_id = wp_fusion()->crm->add_object( $data, 'Lead' );
#Update an Event object in Zoho when an Event post type is updated in WordPress
// Runs on any post with post type "event" and updates the "Event" custom object with values Title and EventDate

function create_update_event_object( $post_id, $post, $update ) {

// Don't run if WP Fusion isn't active, otherwise you'll get an error

if ( ! function_exists( 'wp_fusion' ) ) {
return;
}

// This is the data to be sent to the CRM

$event_data = array(
'Title' => $post->post_title,
'EventDate' => get_post_meta( $post_id, 'event_date', true )
);

// See if this object has already been synced

$object_id = get_post_meta( $post_id, wp_fusion()->crm->slug . '_event_id', true );

if ( empty( $object_id ) {

// New event

$object_id = wp_fusion()->crm->add_object( $event_data, 'Event' );

if ( ! is_wp_error( $object_id ) ) {

// Save the ID of the new record for future updates.

update_post_meta( $post_id, wp_fusion()->crm->slug . '_event_id', $object_id );

} else {

// Error, log it.

wpf_log( 'error', 0, 'Error creating event:' . $object_id->get_error_message() );

}

} else {

// Existing event

wp_fusion()->crm->update_object( $object_id, $event_data, 'Event' );

}

}

// save_post_event runs whenever an "event" post type is created or updated (see https://developer.wordpress.org/reference/hooks/save_post_post-post_type/)

add_action( 'save_post_event', 'create_update_event_object', 10, 3 );
#Create a custom Car object in HubSpot and associate it with a contact
For more information on working with custom objects in HubSpot, see Custom Objects with HubSpot.
define( 'HUBSPOT_API_KEY', 'xx599590-7888-43ed-a896-5abbc2ef9aa2' );

$properties = array(
'condition' => 'used',
'date_received' => '1582416000000',
'year' => '2014',
'make' => 'Nissan',
'model' => 'Frontier',
'vin' => '4Y1SL65848Z411439',
'color' => 'White',
'mileage' => '80000',
'price' => '12000',
'notes' => 'Excellent condition. No accidents.',
);

$object_type_id = '2-4370788';

$object_id = wp_fusion()->crm->add_object( $properties, $object_type_id );

if ( is_wp_error( $object_id ) ) {
wpf_log( 'error', wpf_get_current_user_id(), 'Error adding object: ' . $object_id->get_error_message() );
return false;
}

// Do what you want with $object_id here.

// For example to associate it with a contact (https://developers.hubspot.com/docs/api/crm/crm-custom-objects).

$contact_id = '101';
$association_type_id = '3';

$request = "https://api.hubapi.com/crm/v3/objects/{$object_type_id}/{$object_id}/associations/contacts/{$contact_id}/{$association_type_id}/?hapikey=" . HUBSPOT_API_KEY;

$params = array( 'method' => 'PUT' );
$response = wp_safe_remote_request( $request, $params );

if ( is_wp_error( $response ) ) {
wpf_log( 'error', wpf_get_current_user_id(), 'Error associating object with contact: ' . $response->get_error_message() );
return false;
}

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

apply_tags()

apply_tags()

#Overview
This function allows you to apply an array of tags to a user.
The function expects an array of tag IDs. Some CRM』s, like Infusionsoft, Ontraport, and ConvertKit use internal tag IDs to designate their tags. Other CRMs, like ActiveCampaign, Drip, AgileCRM, and Mautic don』t use tag IDs, and a tag label is sufficient.
If you』re unsure, you can always use get_tag_id() to get the appropriate tag ID for this function:
$tag_id = wp_fusion()->user->get_tag_id( 'Tag Name' );
#Apply tags to the current user (Infusionsoft and others which use tag IDs)
$tags = array(123, 456, 789);
wp_fusion()->user->apply_tags( $tags );
#Apply tags to the current user (ActiveCampaign and others which don』t use tag IDs)
$tags = array('Tag One', 'Tag Two', 'Tag Three');
wp_fusion()->user->apply_tags( $tags );
#Apply tags to a specific user ID
$tags = array(123, 456, 789);
wp_fusion()->user->apply_tags( $tags, $user_id );
The function will return true if the tags were applied successfully, and false if there was a connection error or the user wasn』t found in the CRM.

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No

get_contact_id()

get_contact_id()

#Overview
This function retrieves a user』s CRM contact ID based on their WordPress user ID.
#Get the current user』s contact ID
$contact_id = wpf_get_contact_id();
#Get the contact ID for a different user
$contact_id = wpf_get_contact_id( $user_id );
#Force an update of the user』s contact ID by sending an API call to your CRM
In this case the updated contact ID will automatically be saved to the local user meta after it』s been retrieved.
$contact_id = wpf_get_contact_id( $user_id, true );

#Was this helpful?

Let us know if you liked the post. That』s the only way we can improve.

Yes

No