Adrian Esquivel
November 18, 2010
Articles

Highrise & Cforms II - A story of integration

There is an excellent plugin available by the name of WP Highrise Contact which gives users the ability to insert a quick tag and generate a form on any page, normally this would be more than sufficient for CRM Integration but a lot of us love Cforms for our forms. Recently the need to manage incoming leads in a more efficient manner was needed and we'd been using Cforms and saw WP Highrise and thought that the two could be made to work together, the end result is a bit on the "hackish" but saved us the work of developing a custom plugin to hook into Cforms. This is how.First we need to examine the way that Cforms sends data,

2010-11-18_1522

We see that data is sent according to the way that the form has been setup in Cform's Administration panel. While we're looking at this image, notice cf7_field_7 it's a bit odd and it's what's responsible for mapping out Cform fields with WP Highrise Contact.

2010-11-18_1526

By default the WP Highrise Contact plugin accepts only a person's name, a company, an email, a phone number and a website as well as a message. Here we see that our first input field is "Your name" and would map to name. The second is "Your Orginization" and would map to company.

  • Your Name -> name
  • Your Organization -> company
  • Your Email Address -> email
  • Your Telephone Number -> phoneNumber
  • Your Website -> website
  • Message -> message

Now that we've defined the basic syntax for mapping your cform fields to make them work with WP Highrise Contact then we get to the code modifications that must be done. When you've installed the WP Highrise Contact plugin open up the file named wp-highrise-contact.php and look for a code block that looks like the following:if (!empty($_POST['honeytrap'])) { /** * Send notification email * * By default, this is commented out. Simply remove the comments to enable it and reeive a * notifiation when a robot fills the form. Yes, I know, it defeats the purpose, but it's only * if you want to see how the Honey Pot spam control is effective :-) */ /*$to = get_option('wphc_email_address'); $subject = 'SPAM - ' . get_option('wphc_email_subject'); $body = print_r($_POST, true); $body .= "rn"; $body .= 'Form: ' . get_permalink() . "rn"; wp_mail($to, $subject, $body); */ header('location: ' . get_bloginfo('url')); die;}Right after this conditional block we'll add the following loop to sort out the Cforms data that actually contains data: /** Convert post fields to our highrise compatible entries **/ $cforms = array(); foreach($_POST as $k => $value) { if(strstr($k, 'field_')) { $cforms[] = $value; } }Because of the way we've added the field in Cforms the hidden field containing our mapping is the very last, from here it's a simple matter of association so we'll add this right after the above block. $cfCount = count($cforms); $mapping = explode('^', $cforms[$cfCount - 1]); for($i = 0; $i < $cfCount - 1; ++$i) { ${$mapping[$i]} = filter_var($cforms[$i], FILTER_SANITIZE_SPECIAL_CHARS); }Via this process we've made some lines redundant, so we can remove this. This the block we don't need anymore. foreach ( array_keys( $_POST) as $k ) { $$k = filter_input( INPUT_POST, $k, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW ); }In our forms we normally only specify one field for the form and Highrise has a firstName and lastName field so we need to split up the name and feed it accordingly. Look for this block:if ($highrise_connected) { $contact = new inbox_highrise_Contact();Immediately afterwards we'll insert this block of code: if($name) { $namePieces = strstr($name, ' ') ? explode(' ', $name) : array($name, ''); $firstName = $namePieces[0]; $lastName = $namePieces[1]; }What we've just done is use a ternary operation to check if we have two word blocks, presumably your first and last name. If not, we'll assume we only have a first name. Then we'll assign them to the variables the plugin uses normally.The only thing left to do is stop the plugin from replacing the quicktag with the form since we've let Cforms handle the form generation, look for these two blocks:if (file_exists(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php')) $form = include_once(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php'); else $form = include_once(dirname( __FILE__ ) . '/form.inc.php'); $content = str_replace('[wp-highrise-contact]', $form, $content);replace this one with:$content = str_replace('[wp-highrise-contact]', '', $content);if (file_exists(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php')) $form = include_once(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php'); else $form = include_once(dirname( __FILE__ ) . '/form.inc.php'); return str_replace('[wp-highrise-contact]', $form, $content)replace this one with:return str_replace('[wp-highrise-contact]', '', $content);