wpf_salesforce_query_args

wpf_salesforce_query_args

#Overview
WP Fusion uses the Salesforce Object Query Language (SOQL) to search for records in your Salesforce account.
For example when searching for a contact record by email address, loading the tags or topics for a contact, and applying / removing tags or topics from a contact.
Before WP Fusion executes any Salesforce query, the query is passed through the wpf_salesforce_query_args filter, which allows you to modify the query before it』s sent.
#Parameters

$query_args (array): The query arguments
$method (string): The API method being performed
$searchfield (string): The value being searched for

The default query arguments, by method, are:
Method: get_contact_id
array( "q" => "SELECT Id from Contact WHERE Email = '{$email_address}'" )
Method: get_topics
array( "q" => "SELECT TopicId from TopicAssignment WHERE EntityId = '{$contact_id}'" );
Method: load_contacts
array( "q" => "SELECT EntityId from TopicAssignment WHERE TopicId = '{$topic_id}'" );
#Examples
#Use a custom email field for contact lookups
By default WP Fusion uses the Email field for looking up a contact ID. This example changes that lookup field to a custom field on a custom object type, Email__c:
function custom_wpf_query_args( $query_args, $method, $searchfield ) {

if ( 'get_contact_id' == $method ) {

// In this case the $searchfield is the email address we're trying to get the contact ID of
// The default value of wp_fusion()->crm->object_type is Contact

$query_args['q'] = "SELECT Id from {wp_fusion()->crm->object_type} WHERE Email__c = '{$searchfield}'";

}

return $query_args;
}

add_filter( 'wpf_salesforce_query_args', 'custom_wpf_query_args', 10, 3 );

#Was this helpful?

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

Yes

No

wpf_redirect_url

wpf_redirect_url

#Overview
This filter is run when a user is redirected from a restricted post or page. It can be used to override the redirect configured in the WP Fusion meta box. To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$redirect: Represents the URL the user will be redirected to.
$post_id: Post ID for the post being requested.

#Examples
#Send logged out users to a login page, and send logged-in users to upsell pages based on their applied tags
Say you have a page, 「Level 2 Membership Content」. This page has 「Restrict Access」 checked and requires the 「Level 2 Member」 tag to view the page. Configure the redirect in the meta box settings on the 「Level 2 Membership Content」 page to point your login page.
This will redirect all users who either don』t have the 「Level 2 Member」 tag, or any logged out users, to the login page. However if a user is already logged in, we may want to modify the redirect depending on their membership level.
The code below redirects users who don』t have the tag 「Level 1 Member」 to the sales page for Level 1 Membership. It redirects users who don』t have the tag 「Level 2 Member」 to the sales page for Level 2 Membership.
function wpf_redirect_users( $redirect, $post_id ) {

// Go to the default redirect (the login page) for non-logged in users
if ( ! wpf_is_user_logged_in() ) {
return $redirect;
}

// If they don't have the tag Level 2 Member, go to the Buy Level 2 Membership page
if ( ! wpf_has_tag( 'Level 2 Member' ) ) {
return 'http://mysite.com/buy-level-2-membership';
}

// If they don't have the tag Level 1 Member, go to the Buy Level 1 Membership page
if ( ! wpf_has_tag( 'Level 1 Member' ) ) {
return 'http://mysite.com/buy-level-1-membership';
}

}

add_filter( 'wpf_redirect_url', 'wpf_redirect_users', 10, 2 );

#Was this helpful?

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

Yes

No

wpf_query_filter_get_posts_args

wpf_query_filter_get_posts_args

#Overview
WP Fusion』s Filter Queries option allows you to modify the results of database queries to exclude content that a user can』t access (based on WP Fusion』s access rules).
When used in Advanced mode, Filter Queries will build up a list of posts a user has access to before the main query is run, and add it to the main query via the post__not_in parameter (in order to exclude any restricted posts).
The wpf_query_filter_get_posts_args filter lets you modify the arguments going into that query.
#Parameters

$args (array): WP_Query arguments

#Examples
#Pre-query more posts
To protect the stability of your site, the pre-query will load at most 200 restricted posts. However if you have more than 200 posts of that post type which are protected by WP Fusion』s access rules, this could cause users to see posts they don』t have access to.
The example below extends the pre-query to check the first 1000 restricted posts.
Note that this can cause multiple thousands of extra database queries per page load, so it should be used with extreme caution.
function filter_query_posts_args( $args ) {

$args['posts_per_page'] = 1000;

return $args;

}

add_filter( 'wpf_query_filter_get_posts_args', 'filter_query_posts_args' );
#Activate Ludicrous Mode
By default, the pre-query only looks at posts that have WP Fusion access rules saved to their postmeta.
This provides improved performance, but it means that posts protected by post type rules or taxonomy term rules will not necessarily be excluded from the results, even in Advanced mode.
This snippet removes the meta_query and also sets the posts_per_page to 5000. Effectively this means that every post of the post type will be pre-checked for access before the query is run.
If you have a lot of posts and/or taxonomy terms, this will almost certainly crash your site. Use with caution.
function filter_query_ludicrous_mode( $args ) {

$args['posts_per_page'] = 5000;
unset( $args['meta_query'] );

return $args;

}

add_filter( 'wpf_query_filter_get_posts_args', 'filter_query_ludicrous_mode' );
This functionality could be made more performant by running it on a schedule (or as content is published) and building up a cache of the access list for every user on the site, ideally in a custom database table. However that is beyond the scope of this article.

#Was this helpful?

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

Yes

No

wpf_pulled_user_meta

wpf_pulled_user_meta

#Overview
This filter allows you to modify a user』s meta data whenever it has been pulled from your CRM, and before it is saved to the database. To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$user_meta: Array of user meta data, in the format array('meta_field' => 'value').
$user_id: ID of the user being updated

#Examples
#Update a user』s Display Name based on their first and last name
If you』re using the Display Name field on your site to show post authors, create member directories, or elsewhere in your templates, you may want to automatically update a user』s display name if their name is changed in your CRM.
function set_display_name( $user_meta, $user_id ) {

$user_meta['display_name'] = $user_meta['first_name'] . ' ' . $user_meta['last_name'];

return $user_meta;

}

add_filter( 'wpf_pulled_user_meta', 'set_display_name', 10, 2 );
#Limit the fields that are loaded from the CRM
By default WP Fusion will load any fields from the CRM that are enabled for sync from the Contact Fields tab. This example lets you limit the loaded data to name and email address, while ignoring any other data.
function limit_loaded_metadata( $user_meta, $user_id ) {

$limited_meta = array(
'first_name' => $user_meta['first_name'],
'last_name' => $user_meta['last_name'],
'user_email' => $user_meta['user_email'],
);

return $limited_meta;

}

add_filter( 'wpf_pulled_user_meta', 'limit_loaded_metadata', 10, 2 );

#Was this helpful?

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

Yes

No

wpf_post_type_rules

wpf_post_type_rules

#Overview
This filter lets you apply WP Fusion』s access controls to an entire post type.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$rules: An array of post types and their access control rules

#Rule properties
The array properties for the access rules are the same as those saved on an individual post. They are:

lock_content: (bool) The user must be logged in to access the content
allow_tags: (array) The user must have at least one of the specified tags to access the content. Tag IDs must be used if the CRM uses tag IDs. If you』re not sure, use wpf_get_tag_id( $name ) to get the ID.
allow_tags_all: (array) The user must have all of the tags to access the content
allow_tags_not: (array) The user must have none of the tags to access the content
redirect: (int) A post ID to redirect the user to if access is denied. Leave blank to display the Restricted Content Message
redirect_url: (string) Optionally specify a remote URL to redirect the user to if access is denied. Will take priority over redirect

#Examples
The example below will protect a custom post type with slug my_type and redirect the user if they don』t have the required tags.
function wpf_post_type_rules( $rules ) {

$rules['my_type'] = array(
'lock_content' => true,
'allow_tags' => array( 'Tag One' ), // Array of required tags (any). Must use tag IDs if the CRM uses tag IDs
'allow_tags_all' => array( 'Tag Two', 'Tag Three' ), // Array of required tags (all)
'allow_tags_not' => array( 'Payment Failed' ), // Array of required tags (not)
'redirect' => 123, // Post ID to redirect to
'redirect_url' => 'https://example.com/', // OR enter a URL (overrides post ID redirect)
);

return $rules;

}

add_filter( 'wpf_post_type_rules', 'wpf_post_type_rules' );

#Was this helpful?

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

Yes

No

wpf_meta_fields

wpf_meta_fields

#Overview
This filter lets you register custom meta fields to show up in the Contact Fields list. From there you can map them to a field in your CRM, and sync data either manually using push_user_meta() or automatically by registering the field using wpf_watched_meta_fields.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$fields: An array of fields to add

#Examples
The example below will add a new field for sync with key primary_membership.
function add_custom_meta_field( $meta_fields ) {

$meta_fields['primary_membership'] = array( 'label' => 'Membership', 'type' => 'text', 'group' => 'wordpress' );

return $meta_fields;

}

add_filter( 'wpf_meta_fields', 'add_custom_meta_field' );

#Was this helpful?

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

Yes

No

wpf_import_user

wpf_import_user

#Overview
This filter runs when a new user is being imported, either via the import tool or a webhook. It allows you to modify a user』s meta data after it』s been loaded from the CRM, but before the new user has been inserted.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$user_meta: Array of user meta data, in the format array('meta_field' => 'value').
$contact_id: CRM contact ID of the user being imported

#Examples
#Set imported users to have usernames generated from their first and last names, instead of email address
function wpf_use_names_as_logins( $user_meta ) {

$user_meta['user_login'] = strtolower( $user_meta['first_name'] . $user_meta['last_name'] );

return $user_meta;

}

add_filter( 'wpf_import_user', 'wpf_use_names_as_logins' );
#Set imported users to have usernames generated from their first name and a random number, instead of email address
function wpf_use_names_as_logins( $user_meta ) {

$user_meta['user_login'] = strtolower( $user_meta['first_name'] ) . rand(10, 1000);

return $user_meta;

}

add_filter( 'wpf_import_user', 'wpf_use_names_as_logins' );

#Was this helpful?

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

Yes

No

wpf_forms_pre_submission

wpf_forms_pre_submission

#Overview
This filter is run when WP Fusion is processing a form submission from one of our supported form plugins.
It is triggered after the initial contact ID lookup in the CRM, but before any data has been synced. It can be used to modify the data that』s sent to the CRM.
To use the code examples below, add them to your active theme』s functions.php file.
#Alternate filters
For more precise targeting there are two alternate filters with the same arguments:

wpf_{integration slug}_pre_submission : Where {integration_slug} is the name of the form integration, for example wpf_gform_apply_tags

#Parameters

$update_data (array): This is an array of data that will be synced to the CRM
$user_id (int): The user who submitted the form, 0 if a guest
$contact_id (string): The ID of the contact record created / updated by the form submission
$form_id (int): The ID of the submitted form

#Examples
#Append to a field instead of overwriting it
In this case, we have a form field mapped to the ContactNotes field in Infusionsoft, but instead of replacing the notes, we』d like to update them.
This example runs when a form is submitted and first loads the contact』s ContactNotes from Infusionsoft. If there are already notes on the contact record, the new notes are appended to the existing notes before being sent back to the CRM.
function append_to_notes_field( $update_data, $user_id, $contact_id, $form_id ) {

if ( ! empty( $update_data['ContactNotes'] ) && false !== $contact_id ) {

// If we're updating an existing contact and there are notes in the data

wp_fusion()->crm->connect(); // Set up the Infusionsoft API

$result = wp_fusion()->crm->app->loadCon( $contact_id, array( 'ContactNotes' ) ); // Load the contact

if ( ! is_wp_error( $result ) && ! empty( $result['ContactNotes'] ) ) {

// Append the new notes to the existing notes (PHP_EOL adds a line break)
$update_data['ContactNotes'] = $result['ContactNotes'] . PHP_EOL . $update_data['ContactNotes'];
}
}

return $update_data;

}

add_filter( 'wpf_forms_pre_submission', 'append_to_notes_field', 10, 4 );

#Was this helpful?

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

Yes

No

wpf_forms_pre_submission_contact_id

wpf_forms_pre_submission_contact_id

#Overview
This filter is run when WP Fusion is processing a form submission from one of our supported form plugins.
It is triggered after WP Fusion has attempted to locate a contact ID in the CRM for the form submission, but before a contact record is created / updated, and before any tags are applied.
To use the code examples below, add them to your active theme』s functions.php file.
#Alternate filters
For more precise targeting there are two alternate filters with the same arguments:

wpf_{integration slug}_pre_submission_contact_id : Where {integration_slug} is the name of the form integration, for example wpf_gform_pre_submission_contact_id

#Parameters

$contact_id (string|false): The contact ID in the CRM to be updated, or false if no match found
$update_data (array): The data about to be synced to the CRM, in key => value pairs
$user_id (int): The ID of the registered user who submitted the form, or 0 for guest
$form_id (int): The ID of the submitted form

#Examples
#Force all form submissions to create a new contact record
This is a simple example that stops WP Fusion from trying update existing contact records in the CRM from form submissions. All form submissions will create a new contact.
add_filter( 'wpf_forms_pre_submission_contact_id', '__return_false' );
#Force all form submissions to create a new contact record by form ID
This is the same as the example above but only runs on form IDs 22 and 29
function always_create_new_contacts( $contact_id, $update_data, $user_id, $form_id ) {

if ( 22 == $form_id || 29 == $form_id ) {
$contact_id = false;
}

return $contact_id;

}

add_filter( 'wpf_forms_pre_submission_contact_id', 'always_create_new_contacts', 10, 4 );
#Use custom lookup logic for contact records – Infusionsoft
By default WP Fusion uses the first email address found on a submitted form when determining whether to create or update a contact record in your CRM.
In some cases you may want to use more complex rules for determining when a new record should be created.
This example for Infusionsoft attempts to match against the Email Address 2 and Email Address 3 fields in addition to the Email Address field. That means that if the form is submitted and the entered email matches the Email Address 3 field on an existing contact, that contact record will be updated (rather than a new contact record being added).
function wpf_lookup_additional_emails( $contact_id, $update_data, $user_id, $form_id ) {

if ( empty( $contact_id ) ) {

$email = $update_data['Email'];

wp_fusion()->crm->connect();

$query = array( 'EmailAddress2' => $email );
$result = wp_fusion()->crm->app->dsQuery( 'Contact', 1, 0, $query, array( 'Id' ) );

if ( isset( $result[0]['Id'] ) ) {

return $result[0]['Id'];

}

$query = array( 'EmailAddress3' => $email );
$result = wp_fusion()->crm->app->dsQuery( 'Contact', 1, 0, $query, array( 'Id' ) );

if ( isset( $result[0]['Id'] ) ) {

return $result[0]['Id'];

}
}

return $contact_id;

}

add_filter( 'wpf_forms_pre_submission_contact_id', 'wpf_lookup_additional_emails', 10, 4 );
#Use custom lookup logic for contact records – Salesforce
This example for Salesforce only updates an existing contact record if the email address, first name, and last name match. Otherwise a new record will be created.
function wpf_forms_name_lookup( $contact_id, $update_data, $user_id, $form_id ) {

$params = wp_fusion()->crm->get_params(); // get the authentication headers and other API params

// URL-encode the three lookup fields

$email_address = urlencode( $update_data['Email'] );
$first_name = urlencode( $update_data['FirstName'] );
$last_name = urlencode( $update_data['LastName'] );

// this is the SOQL, Salesforce Object Query Language:

$query_args = array(
'q' => "SELECT Id from {wp_fusion()->crm->object_type} WHERE Email = '{$email_address}' AND FirstName = '{$first_name}' AND LastName = '{$last_name}'",
);

$request = add_query_arg( $query_args, wp_fusion()->crm->instance_url . '/services/data/v42.0/query' );
$response = wp_remote_get( $request, $params );

if ( is_wp_error( $response ) ) {

// If an error was encountered, log it
wpf_log( 'error', $user_id, 'Error looking up Salesforce contact: ' . $response->get_error_message() );
return false;

}

$response = json_decode( wp_remote_retrieve_body( $response ) );

if ( empty( $response ) || empty( $response->records ) ) {

// If no match was found, return false so a new contact is created
return false;

}

// Return the ID of the contact that matched the email, first name, and last name
// It's this contact ID that will be updated by the form submission

return $response->records[0]->Id;

}

add_filter( 'wpf_forms_pre_submission_contact_id', 'wpf_forms_name_lookup', 10, 4 );

#Was this helpful?

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

Yes

No

wpf_forms_apply_tags

wpf_forms_apply_tags

#Overview
This filter is run when WP Fusion is processing a form submission from one of our supported form plugins.
It is triggered after the contact record has been created / updated in the CRM, but before any tags have been applied. It can be used to modify the tags that will be applied as part of the form submission.
To use the code examples below, add them to your active theme』s functions.php file.
#Alternate filters
For more precise targeting there are two alternate filters with the same arguments:

wpf_{integration slug}_apply_tags : Where {integration_slug} is the name of the form integration, for example wpf_gform_apply_tags
wpf_{integration slug}_apply_tags_{form_id} : Where {form_id} is the ID of the submitted form, for example wpf_ninja_forms_apply_tags_1

#Parameters

$apply_tags (array): This is an array of tags that will be applied to the person who submitted the form
$user_id (int): The user who submitted the form, 0 if a guest
$contact_id (string): The ID of the contact record created / updated by the form submission
$form_id (int): The ID of the submitted form

#Examples
#Apply an additional tag based on a cookie
This example checks to see if the person submitting the form came to the site via an AffiliateWP affiliate link, and is being tracked with the affwp_refcookie. If so, the tag Affiliate Tag is applied.
function merge_affiliate_tags( $apply_tags, $user_id, $contact_id, $form_id ) {

if ( isset( $_COOKIE['affwp_ref'] ) ) {
$apply_tags[] = 'Affiliate Tag';
}

return $apply_tags;

}

add_filter( 'wpf_forms_apply_tags', 'merge_affiliate_tags', 10, 4 );
#Use 「Round Robin」 assignment for leads
This example randomly assigns each lead a tag of either Tag A, Tag B or Tag C. This is similar to the 「Round Robin」 owner assignment in sales CRMs like Infusionsoft, and can be used to randomly assign leads to a sales rep.
function apply_round_robin_tags( $apply_tags ) {

// To properly randomize the tags we'll do it based on the current time

$time = date( 's' );

// $time will be 00 to 59

if ( $time = 20 && $time < 40 ) {
$apply_tags[] = 'Tag B';
} elseif ( $time < 60 ) {
$apply_tags[] = 'Tag C';
}

return $apply_tags;

}

add_filter( 'wpf_forms_apply_tags', 'apply_round_robin_tags' );

#Was this helpful?

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

Yes

No