Overview
When integrating an Act-On Form external post with WordPress, WP-Members is a popular, free plug-in to create a membership framework for your WordPress site if you hope to restrict content access to registered members only.
Please note that these integration steps are intended for web developers, and it involves making edits to the WordPress functions.php file.
See Integrating an Act-On Form External Post with WordPress for other WordPress form plug-ins.
Setup Steps
Step 1:
For each form this is used with, we need an equivalent "mirror" version of the Form to be created within Act-On. For example, if your custom form on your website captures First Name, Last Name, and Email, you'll need to create a form within Act-On that also requests these fields. Your website visitors will never see this Act-On variant of your form - we simply need to receive your custom form’s data.
In each field in your Act-On "proxy" form, note the "Data Field Name". You can see the Data Field Name by clicking each form field in the form editor to bring up the field settings. We will be needing this Data Field Name in the functions.php code addition below.
Step 2:
Get your Act-On form's external post URL. This is located under Content > Form Post URLs. The external post URL is unique from your form's public URL, and is designed specifically for accepting incoming external submissions.
Step 3:
Paste the following code into a valid spot of your theme's functions.php file:
Class to implement:
/** * Post form submission data to Act-On and convert visitors to known via cURL, allowing no direct touch * between Act-On and your website vistitor's browser to be necessary */ class ActonWordPressConnection { protected $_postItems = array(); protected function getPostItems() { return $this->_postItems; } /** * for setting your form's POST items (key is your form input's name, value is the input value) * @param string $key first part of key=value for form field submission (name in name=John) * @param string $value latter part of key=value for form field submission (John in name=John) */ public function setPostItems($key, $value) { $this->_postItems[$key] = (string)$value; } protected function getDomain($address) { $pieces = parse_url($address); $domain = isset($pieces['host']) ? $pieces['host'] : ''; if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { return $regs['domain']; } return false; } // get IP of website visitor to send to Act-On for location info protected function getUserIP() { // check proxy if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } /** * process form data for submission to your Act-On external form URL * @param string $extPostUrl your external post (Proxy URL) for your Act-On "proxy" form */ public function processConnection($extPostUrl) { // get the account ID from $extPostURL $acctIdWithPath = preg_replace('/^(.*?)eform\//', '', $extPostUrl); // remove extPostUrl string parts up to 'eform/' $acctId = explode('/', (string)$acctIdWithPath, 2); // remove parts after the first /, which leaves the acct ID remaining $aoCookieName = 'wp' . $acctId[0]; $aoCookieNameOI = 'ao_optin' . $acctId[0]; // if opt-in cookie is enabled if (isset($_COOKIE[$aoCookieName])) { $aoCookieToSend = new WP_Http_Cookie(); $aoCookieToSend->name = $aoCookieName; $aoCookieToSend->value = $_COOKIE[$aoCookieName]; $aoCookiesToSend[] = $aoCookieToSend; if (isset($_COOKIE[$aoCookieNameOI])) { $aoCookieToSendOI = new WP_Http_Cookie(); $aoCookieToSendOI->name = $aoCookieNameOI; $aoCookieToSendOI->value = $_COOKIE[$aoCookieNameOI]; $aoCookiesToSend[] = $aoCookieToSendOI; } $this->setPostItems('_ipaddr', $this->getUserIP()); // Act-On accepts manually defined IPs if using field name '_ipaddr' $fields = http_build_query($this->getPostItems()); // encode post items into query-string $request = wp_remote_get($extPostUrl . '?' . $fields, array( 'cookies' => $aoCookiesToSend )); $aoResponseCookie = explode(";", (string)$request["headers"]["set-cookie"]); foreach($aoResponseCookie as $key => & $value) { $splitAtEquals = explode('=', (string)$value); $newKey = $splitAtEquals[0]; // set array keys to named keys (wpXXXX, Version, Domain, Max-Age, Expires, Path) $aoResponseCookie[$newKey] = $value; $newValue = preg_replace('/^(.*?)=/', '', $value); $value = $newValue; } setrawcookie($aoCookieName, $aoResponseCookie[$aoCookieName], time() + 86400 * 365, "/", $this->getDomain($extPostUrl)); } } }
If you do not have a preferred location for custom classes, this can be pasted at the bottom of your functions.php file (but before the closing "?>" tag -- we don't want to remove that!). You only need to paste this code in once.
This is the same class as is utilized for the Gravity forms API integration.
For the class implementation, however, we will want two instances -- one for those registering, and one for those logging in (who have previously registered). This is because WP-Members uses a different hook for each case.
An example implementation of the above class for new registrations from WP-Members:
add_action('init', 'process_post'); function process_post(){ add_action( 'wpmem_post_register_data', 'my_reg_hook', 1 ); function my_reg_hook( $fields ) { $ao_wp_mem_reg = new ActonWordPressConnection; $ao_wp_mem_reg->setPostItems('First Name',$fields[first_name]); // HTML input name attribute is "First Name", Act-On data field name should match that $ao_wp_mem_reg->setPostItems('Last Name',$fields[last_name]); // please note that Act-On does not accept filed names with punctuation marks in them, so rename as necessary $ao_wp_mem_reg->setPostItems('E-mail Address', $fields[user_email]); $ao_wp_mem_reg->processConnection('http://subdomain.domain.com/acton/ef...57/d-ext-0001'); // your external post URL ("Proxy Form URL") } }
Please reference the code notes for the above. Remember to reference your specific input's Data Field Names as the first argument for each setPostItem.
The above uses two different hooks, with the WP-Members registration hook nested inside a WordPress init hook:
- The WordPress init hook is used because we need to set the Act-On cookie value (which must be done inside the HTTP headers).
- Nested inside that, the WP-Members registration hook wpmem_post_register_data fires once the form submission has passed validation and has been written to your WordPress database, but before the WP-Members confirmation email has been sent.
An example implementation of the above class for logins for existing members from WP-Members:
add_action('init', 'process_post'); function process_post(){ add_action( 'wpmem_post_update_data', 'my_reg_hook', 1 ); function my_update_hook( $fields ) { $post1 = new ActonWordPressConnection; $post1->setPostItems('First Name',$fields[first_name]); // HTML input name attribute is "First Name", Act-On data field name should match that $post1->setPostItems('Last Name',$fields[last_name]); // please note that Act-On does not accept filed names with punctuation marks in them, so rename as necessary $post1->setPostItems('E-mail Address', $fields[user_email]); $post1->processConnection('http://subdomain.domain.com/acton/ef...57/d-ext-0001'); // your external post URL ("Proxy Form URL") } }
Please reference the code notes for the above. Remember to reference your specific input's Data Field Names as the first argument for each setPostItem.
The above uses two different hooks, with the WP-Members update hook nested inside a WordPress init hook:
- The WordPress init hook is used because we need to set the Act-On cookie value (which must be done inside the HTTP headers)
- Nested inside that, the WP-Members registration hook wpmem_post_update_data fires once the form submission has passed validation and has been written to your WordPress database, but before the WP-Members confirmation email has been sent
Note: If you are not sure what the field names are for your WP-Members form submissions, place the following line inside the WP-Members hook function and it will print them out for you on your next submission:
echo "<pre>"; print_r( $fields ); echo "</pre>"; /* BELOW: The default printout of the above $fields array, if you use default fields for the WP-members registration form */ Array ( [username] => x [user_email] => x [first_name] => x [last_name] => x [addr1] => x [addr2] => x [city] => x [thestate] => x [zip] => x [country] => x [phone1] => x [password] => x [user_registered] => x [user_role] => x [wpmem_reg_ip] => x [wpmem_reg_url] => x [user_nicename] => x [display_name] => x [nickname] => x [ID] => 16 [user_url] => )
Web Developer Resources
- WP-members: wpmem_post_register_data (outside link)
- WP-members: wpmem_post_update_data (outside link)
- WordPress init hook (outside link)
- WordPress functions.php file explained (outside link)