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_woocommerce_attendee_data

wpf_woocommerce_attendee_data

#Overview
This filter is run during a WooCommerce checkout, while WP Fusion is preparing to sync FooEvents event attendees to your CRM. It can be used to modify the data that is synced for each attendee.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$update_data: (array) This is an array of key value pairs representing WordPress meta fields and their corresponding values.
$attendee: (array) The FooEvents attendee data.
$order_id: (int) The WooCommerce order ID

#Examples
#Sync event dates in separate fields
This example syncs the event date in a separate field, identified by the event ID (like event_date_123).
It can be used to store event dates in multiple custom CRM fields when a customer registers for multiple events simultaneously.
function sync_booking_dates( $update_data, $attendee, $order_id ) {

$product_id = $attendee['WooCommerceEventsProductID'];

$update_data[ 'event_date_' . $product_id ] = get_post_meta( $product_id, 'WooCommerceEventsDate', true );

return $update_data;

}

add_action( 'wpf_woocommerce_attendee_data', 'sync_booking_dates', 10, 3 );

#Was this helpful?

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

Yes

No

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_woocommerce_customer_data

wpf_woocommerce_customer_data

#Overview
This filter is run during a WooCommerce checkout, after WP Fusion has extracted the customer data from the order object. It can be used to sync additional data from a WooCommerce order to custom fields in your CRM.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$customer_data: This is an array of key value pairs representing WordPress meta fields and their corresponding values.
$order: The WooCommerce order object

#Examples
#Sync the WooCommerce total order value to a custom field
function get_order_total( $customer_data, $order ) {

$customer_data['order_total'] = $order->get_total();

return $customer_data;

}

add_filter( 'wpf_woocommerce_customer_data', 'get_order_total', 10, 2 );

#Sync the coupon code used on an order to a custom field
// Add the coupon field as available for sync on the Contact Field tab in the WPF settings

function wpf_coupon_meta_fields( $fields ) {

$fields['wc_coupon'] = array( 'label' => 'Coupon', 'type' => 'text', 'group' => 'woocommerce' );

return $fields;

}

add_filter( 'wpf_meta_fields', 'wpf_coupon_meta_fields' );

// Get the coupon code of the first coupon used on the order and add it to the order data

function wpf_sync_coupon( $customer_data, $order ) {

$coupons = $order->get_coupon_codes();

if( ! empty( $coupons ) ) {
$customer_data['wc_coupon'] = $coupons[0];
}

return $customer_data;

}

add_filter( 'wpf_woocommerce_customer_data', 'wpf_sync_coupon', 10, 2 );
#Get a custom date field off an order product and sync it to the contact record
// Add the date field as available for sync on the Contact Field tab in the WPF settings

function wpf_product_meta_fields( $fields ) {

$fields['course_date'] = array( 'label' => 'Course Date', 'type' => 'date', 'group' => 'woocommerce' );

return $fields;

}

add_filter( 'wpf_meta_fields', 'wpf_product_meta_fields' );

// Get the date field off the product and merge it into the customer data

function get_product_data( $customer_data, $order ) {

foreach ( $order->get_items() as $item ) {

$product_id = $item->get_product_id();

$meta_value = get_post_meta( $product_id, 'course_date_field_key', true );

if ( ! empty( $meta_value ) ) {
$customer_data['course_date'] = $meta_value;
}
}

return $customer_data;

}
add_filter( 'wpf_woocommerce_customer_data', 'get_product_data', 10, 2 );
#Sync the customer』s lifetime value to a custom field
function example_sync_lifetime_value_with_order( $customer_data, $order ) {

$customer_orders = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => wc_get_is_paid_statuses(),
'meta_key' => '_billing_email',
'meta_value' => $customer_data['billing_email'],
'orderby' => 'ID',
'order' => 'DESC',
)
);

$customer_data['lifetime_value'] = 0;

if ( ! empty( $customer_orders ) ) {

foreach ( $customer_orders as $order_id ) {

$order = wc_get_order( $order_id );

$order_total = $order->get_total();

$customer_data['lifetime_value'] += floatval( $order_total );

}
}

return $customer_data;
}

add_filter( 'wpf_woocommerce_customer_data', 'example_sync_lifetime_value_with_order', 10, 2 );
#Ignore an order
You can return an empty value from the wpf_woocommerce_customer_data filter in order to have WP Fusion ignore a WooCommerce order. In this example we』re going to ignore orders unless their status is completed:
function wpf_only_allow_completed( $customer_data, $order ) {

if ( 'completed' !== $order->get_status() ) {
return null;
}

return $customer_data;

}

add_filter( 'wpf_woocommerce_customer_data', 'wpf_only_allow_completed', 10, 2 );
#Only sync existing customers
This example will only sync the order data to your CRM if the customer is logged in and already has a CRM contact record. If it』s a new customer they will not be synced.
function only_sync_existing_contacts( $customer_data, $order ) {

if ( empty( wpf_get_contact_id( $order->get_user_id() ) ) ) {
return false;
}

return $customer_data;

}

add_filter( 'wpf_woocommerce_customer_data', 'only_sync_existing_contacts', 10, 2 );

#Was this helpful?

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

Yes

No

wpf_should_filter_query

wpf_should_filter_query

#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).
There are some situations where you may need to bypass query filtering on a specific post type or in a certain context, in that case you can use the wpf_should_filter_query filter.
#Parameters

$should_filter (bool): Whether or not to apply query filtering to the query
$query (WP_Query): The query

#Examples
#Bypass query filtering on the home page if the post type is an event
This example bypasses query filtering on the The Events Calendar events list if the current page is the home page.
function bypass_filter_queries_for_events( $should_filter, $query ) {

if ( is_front_page() && 'tribe_events' == $query->get( 'post_type' ) ) {
return false;
}

return $should_filter;

}

add_filter( 'wpf_should_filter_query', 'bypass_filter_queries_for_events', 10, 2 );

#Was this helpful?

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

Yes

No

wpf_woocommerce_subscription_status_apply_tags

wpf_woocommerce_subscription_status_apply_tags

#Overview
This filter is run during a WooCommerce subscription status change, before WP Fusion has applied any tags to the customer.  It can be used to modify the tags that will be applied to the customer.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$apply_tags: This is an array of tags that will be applied to the customer
$status: The current subscription status (i.e. active, pending-cancel, on-hold)
$subscription: The subscription object

#Examples
#Don』t apply any tags when a subscription status changes to active
This is useful if you need to manually approve new orders and you don』t want any tags applied at checkout that would unlock other content.
function custom_wpf_status_check( $apply_tags, $status, $subscription ) {

if ( 'active' == $status ) {
return false;
}

return $apply_tags;

}

add_filter( 'wpf_woocommerce_subscription_status_apply_tags', 'custom_wpf_status_check', 10, 3 );

#Was this helpful?

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

Yes

No

wpf_user_can_access

wpf_user_can_access

#Overview
This filter is run when determining whether a user can access any piece of content on your site that can be protected by WP Fusion, including:

Posts
Pages
Custom post types
Widgets
Content protected by shortcodes
Gutenberg Blocks
Page builder modules (like Beaver Builder, Elementor, Oxygen, and Divi)

You can use this filter to create your own dynamic access rules based on criteria that aren』t available through WP Fusion』s meta boxes.
To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

$can_access: This variable represents whether or not the user can access the content, as determined by your existing access rules. It will either be true or false.
$user_id: ID of the current logged in user. Will be false if the user isn』t logged in.
$post_id: Post ID for the post being requested. Will be false if the item isn』t a post (for example a Gutenberg block)

#See also

wpf_user_can_access_post_type: Runs on a post type, in conjunction with wpf_post_type_rules
wpf_user_can_access_archive: Runs on a taxonomy term ID instead of a post ID, determines if a user has access to a term archive
wpf_user_can_access_widget: Runs on widgets
wpf_user_can_access_block: Runs on Gutenberg blocks

#Examples
#Deny access to posts within a certain category
The example below will allow access to posts in the category 「My Category」 only to users who have the tag 「Special Tag」.
function restrict_post_categories( $can_access, $user_id, $post_id ) {

if ( in_category( 'My Category', $post_id ) && ! wp_fusion()->user->has_tag( 'Special Tag', $user_id ) ) {
return false;
} else {
return true;
}

}

add_filter( 'wpf_user_can_access', 'restrict_post_categories', 10, 3 );

#Restrict past content by registration date
This example denies access to any content that was published before the user』s registration date:
function disallow_before_date_published( $can_access, $user_id, $post_id ) {

$published = get_the_date( 'U', $post_id );
$userdata = get_userdata( $user_id );

if ( strtotime( $userdata->user_registered ) < $published ) {
$can_access = false;
}

return $can_access;

}

add_filter( 'wpf_user_can_access', 'disallow_before_date_published', 10, 3 );
#Restrict past content by date tag applied
As an alternative, you can track the date that a specific tag was applied (for example Paying Member) using the wpf_tags_applied action, and then deny access to any content published before that tag was applied.
This is similar to the functionality with the Restrict Past Content addon by Restrict Content Pro.
function disallow_before_date_published_alt( $can_access, $user_id, $post_id ) {

$published = get_the_date( 'U', $post_id );
$startdate = get_user_meta( $user_id, 'startdate', true );

// If the content was published before the user's startdate field, deny access

if ( empty( $startdate ) || $startdate < $published ) {
$can_access = false;
}

return $can_access;

}

add_filter( 'wpf_user_can_access', 'disallow_before_date_published_alt', 10, 3 );

function update_tag_applied_timestamp( $user_id, $tags ) {

// When TAGNAME is applied, save the current time to the startdate field

if ( in_array( 'TAGNAME', $tags ) ) {
update_user_meta( $user_id, 'startdate', time() );
}

}

add_action( 'wpf_tags_applied', 'update_tag_applied_timestamp', 10, 2 );
#Restrict content by a user』s age
In this example, any post protected by the tag Adult will require the user to be 18 years old or older to view the post.
For this to work you need to collect the user』s birthday in the date_of_birth usermeta field.
function restrict_content_by_age( $can_access, $user_id, $post_id ) {

$settings = get_post_meta( $post_id, 'wpf-settings', true ); // Get the settings

if ( false == $settings || empty( $settings['allow_tags'] ) ) {
return $can_access; // Not protected by WP Fusion
}

$tag_to_check = wpf_get_tag_id( 'Adult' );

if ( in_array( $tag_to_check, $settings['allow_tags'] ) ) {

// The post is protected by the "Adult" tag, check the user's age

$age = strtotime( get_user_meta( $user_id, 'date_of_birth', true ) );

if ( false == $age ) {

// The user's age is unknown, access is denied
return false;

} else {

$minimum_age = strtotime( '+18 years', $age );

if ( time() < $minimum_age ) {

// The user is not 18 or older, access is denied
return false;

} else {

// The user is old enough, access is granted
return true;

}
}
}

return $can_access;

}

add_filter( 'wpf_user_can_access', 'restrict_content_by_age', 10, 3 );
#Requiring an onboarding course completion before accessing the rest of the site
This example uses the presence of an Onboarded tag to lock all content on the site until the user has completed a LearnDash course with ID 123.
Configure the LearnDash course to apply the Onboarded tag when it』s marked complete, and the rest of the site content will then be unlocked.
function require_onboarding( $can_access, $user_id, $post_id ) {

if ( false !== $post_id // If the content being accessed is a post...
&& ! wpf_has_tag( 'Onboarded' ) // and the user doesn't have the tag Onboarded...
&& 123 != learndash_get_course_id( $post_id ) // and the content isn't part of the course ID 123...
) {
return false; //... then access is denied.
}

return $can_access; // Otherwise use the default access rules

}

add_filter( 'wpf_user_can_access', 'require_onboarding', 10, 3 );
#Unlock any locked content when visited from a Facebook link
This example bypasses WP Fusion』s access rules when the visitor is coming to the site from a Facebook link.
function unlock_for_fb_referral( $can_access, $user_id, $post_id ) {

if( isset( $_SERVER['HTTP_REFERER'] ) && strpos( $_SERVER['HTTP_REFERER'], 'facebook') !== false ) {
return true; // http referrer
} elseif ( isset( $_GET['fbclid'] ) ) {
return true; // the ?fbclid= URL parameter
}

return $can_access;

}

add_filter( 'wpf_user_can_access', 'unlock_for_fb_referral', 10, 3 );
#Bypass query filtering on a specific post
When WP Fusion』s query filtering is enabled, any posts that a user doesn』t have access to will be completely hidden from your site, including all listings, course grids, navigation, archives, etc.
There may be cases when you want to exclude a single post from being hidden. For example you might want to show a restricted course in your course catalog, so that when the user clicks on it they』re redirected to a sales page.
By checking if the pre_get_posts action is currently running, we can tell if WP Fusion is filtering the query results, and exclude specific items from being hidden. In this example the post with ID 123 is excluded if the current user has the tag 「Group A」.
function bypass_query_filtering( $can_access, $user_id, $post_id ) {

if ( doing_action( 'pre_get_posts' ) ) {

if ( wpf_has_tag( 'Group A', $user_id ) && 123 == $post_id ) {
$can_access = true;
}
}

return $can_access;

}

add_filter( 'wpf_user_can_access', 'bypass_query_filtering', 10, 3 );
#Unlock everything
Sometimes you might want to temporarily deactivate WP Fusion』s access controls, either for troubleshooting, or a special 「open day」 promotion. This code disables all WP Fusion access rules and grants access to everything.
add_filter( 'wpf_user_can_access', '__return_true' );

#Was this helpful?

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

Yes

No

wpf_woocommerce_user_id

wpf_woocommerce_user_id

#Overview
When WP Fusion processes a WooCommerce checkout for a registered user, it uses the current user』s ID to determine the user』s contact record in your CRM.
That way if a user checks out with a different billing email, their original contact record is properly updated— instead of creating a new contact record and fragmenting your data.
Generally this is desirable, but there may be situations where you don』t want orders to be associated with the current user. For example if your WooCommerce checkouts are being made by agents who are purchasing on behalf of their customers. In that case you would want a new contact record to be created for the customer, rather than updating the contact record of the agent.
The wpf_woocommerce_user_id filter allows you to modify the user ID that WP Fusion uses to determine which user should be associated with the checkout. Returning false from the filter will tell WP Fusion to treat the checkout as a guest checkout.
#Parameters

$user_id (int): The user ID to be used for the checkout
$order (object): The WooCommerce order object

#Examples
#Treat all checkouts as guest checkouts
add_filter( 'wpf_woocommerce_user_id', function( $user_id, $order ) {
return false;
}, 10, 2 );

#Was this helpful?

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

Yes

No

wpf_user_register

wpf_user_register

#Overview
This filter allows you to modify any user meta data captured at registration, before a new contact record is created in your crm. To use the code examples below, add them to your active theme』s functions.php file.
#Parameters

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

#Examples
#Correct an invalid input field name on a registration form
Some plugins allow you to add custom fields to registration forms via code, but the 「name」 value of the input isn』t the same as the meta field in the database. For example, you may have added the field my_custom_field to the form, but in the database (and WP Fusion』s Contact Fields settings) the field is myplugin_my_custom_field. Since WP Fusion is looking for the meta key value as it』s set in the database, it won』t be detected on the registration form.
To correct this, you can use the wpf_user_register filter like so:
function my_wpf_filter_registration( $user_meta, $user_id ) {

if( isset( $user_meta['my_custom_field'] ) ) {
$user_meta['myplugin_my_custom_field'] = $user_meta['my_custom_field'];
}

return $user_meta;

}

add_filter( 'wpf_user_register', 'my_wpf_filter_registration', 10, 2 );

#Preventing a user from being synced to the CRM
If you return null from the wpf_user_register filter, WP Fusion will not create a contact record in your CRM.
In this example we prevent a contact record from being created at registration if the user has the author role:
function my_wpf_filter_registration( $user_meta, $user_id ) {

$user = get_userdata( $user_id );

if ( in_array( 'author', (array) $user->roles ) ) {
return null;
}

return $user_meta;

}

add_filter( 'wpf_user_register', 'my_wpf_filter_registration', 10, 2 );

#Was this helpful?

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

Yes

No

wpf_user_update

wpf_user_update

#Overview
This filter allows you to modify user meta data before it』s sent to your CRM. 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 for the user being updated.

#Examples
#Save the full URL to a user』s profile photo
If you』re using Ultimate Member to run your site』s membership platform, and want to save a link to the user』s profile photo in your CRM, you』ll find that just syncing the profile_photo field gives you the name of the file, but not the full URL. Using wpf_user_update, we can modify this so the full URL to the user』s profile is sent.
add_filter('wpf_user_update', 'set_profile_photo_url', 10, 2);

function set_profile_photo_url($user_meta, $user_id) {

// Changes "profile_photo.png" to "http://mysite.com/wp-content/uploads/ultimatemember/1/profile_photo.png"
if(isset($user_meta['profile_photo']))
$user_meta['profile_photo'] = content_url() . '/uploads/ultimatemember/' . $user_id . '/' . $user_meta['profile_photo'];

return $user_meta;

}
This filter can be used in a wide variety of ways depending on your particular site』s configuration. For assistance in configuring wpf_user_update for your site contact us and we』d be happy to help.

#Was this helpful?

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

Yes

No