wpf_api_{$method_name}

#Overview

This filter allows you to override calls to any of WP Fusion’s CRM API methods. It’s inspired by the get_{$meta_type}_metadata filter in WordPress core.

If a non-null value is returned from this filter, it will bypass sending any API calls to your CRM, and instead use your custom function to handle the API call.

This can be used to override any of WP Fusion’s CRM API methods with your own custom functionality.

#Available method names

For more information see the CRM API documentation.

  • get_contact_id
  • get_tags
  • apply_tags
  • remove_tags
  • add_contact
  • update_contact
  • load_contact
  • load_contacts
  • track_event (with Event Tracking)

#Parameters

#Examples

This example overrides the contact ID lookup with ActiveCampaign so the ID is only returned if the email address, first name, and last name match (for registered users).

/**
 * Get the Contact ID for a given email address, with an additional search by name
 * for registered users.
 *
 * @param null  $result The result. Return a non-null value to override the API call.
 * @param array $args   The arguments passed to the CRM API function.
 * @return string The Contact ID, if found.
 */
function custom_wpf_get_contact_id( $result = null, $args ) {

	// $email_address is the only parameter passed to wp_fusion()->crm->get_contact_id(), so it's stored at $args[0].
	$email_address = $args[0];

	$request_uri = wp_fusion()->crm->api_url . 'api/3/contacts?email=' . rawurlencode( $email_address );

	$user = get_user_by( 'email', $email_address );

	if ( $user ) {

		// If it's a registered user, let's also search by their name.

		$request_uri .= '&search=' . rawurlencode( $user->first_name . ' ' . $user->last_name );

	}

	// Make the API call.

	$response = wp_safe_remote_get( $request_uri, wp_fusion()->crm->get_params() );

	if ( is_wp_error( $response ) ) {
		return $response;
	}

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

	if ( empty( $response->contacts ) ) {
		// No matching contact found.
		return false;
	} else {
		// Return the ID of the contact.
		return $response->contacts[0]->id;
	}
}

add_filter( 'wpf_api_get_contact_id', 'custom_wpf_get_contact_id', 10, 2 );

Was this helpful?