Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Last active December 5, 2019 20:10
Show Gist options
  • Save jtsternberg/9ff8612a65a7fcc320f84a8e6e548684 to your computer and use it in GitHub Desktop.
Save jtsternberg/9ff8612a65a7fcc320f84a8e6e548684 to your computer and use it in GitHub Desktop.
Automatically trigger an OptinMonster form to show, filled out, when an WP Form is submitted.
<?php
/*
* Plugin Name: WP Forms to OptinMonster
* Plugin URI: http://optinmonster.com
* Description: Automatically trigger an OptinMonster form to show, filled out, when an WP Form is submitted.
* Version: 0.1.0
* Author: Justin Sternberg
* Author URI: http://optinmonster.com
*/
add_action( 'plugins_loaded', function() {
global $wpf_om_trigger;
// The WP Form ID.
$form_id = 6632;
// The OptinMonster campaign slug.
$campaign_slug = 'hxojdkdlurbdotsqpfo6';
// The field index for the name field.
$name_field = 0;
// The field index for the email field.
$email_field = 1;
$wpf_om_trigger = new WPF_OM_Trigger( $campaign_slug, $name_field, $email_field );
// Only do this for the Form with an id of 6632.
add_action(
"wpforms_process_complete_{$form_id}",
array( $wpf_om_trigger, 'trigger_campaign_on_success' ),
10,
4
);
} );
class WPF_OM_Trigger {
protected $campaign_slug = '';
protected $name_field = null;
protected $email_field = null;
protected $name = '';
protected $email = '';
/**
* Setup.
*
* @since 0.1.0
*
* @param string $campaign_slug The OptinMonster campaign slug.
* @param int $name_field The WP Forms name field entry.
* @param int $email_field The WP Forms email field entry.
*/
public function __construct( $campaign_slug, $name_field, $email_field ) {
$this->campaign_slug = $campaign_slug;
$this->name_field = $name_field;
$this->email_field = $email_field;
}
/**
* This will fire at the very end of a (successful) form entry.
*
* @since 0.1.0
*
* @link https://wpforms.com/developers/wpforms_process_complete/
*
* @param array $fields Sanitized entry field values/properties.
* @param array $entry Original $_POST global.
* @param array $form_data Form data and settings.
* @param int $entry_id Entry ID. Will return 0 if entry storage is disabled or using WPForms Lite.
*/
public function trigger_campaign_on_success( $fields, $entry, $form_data, $entry_id ) {
// Get the full entry object
$entry = wpforms()->entry->get( $entry_id );
// Fields are in JSON, so we decode to an array
$fields = json_decode( $entry->fields, true );
// WPF name field submitted value.
if ( ! empty( $fields[ $this->name_field ]['value'] ) ) {
$this->name = sanitize_text_field( wp_unslash( trim( $fields[ $this->name_field ]['value'] ) ) );
}
// WPF email field submitted value.
if ( ! empty( $fields[ $this->email_field ]['value'] ) ) {
$this->email = sanitize_text_field( wp_unslash( trim( $fields[ $this->email_field ]['value'] ) ) );
}
// Ok, hook in and add our javascript for OM.
add_action( 'wp_footer', array( $this, 'setup_js_trigger' ) );
}
/**
* Sets up the OptinMonster javascript events to open the campaign and populate with the values.
*
* @since 0.1.0
*
* @return void
*/
public function setup_js_trigger() {
?>
<script type="text/javascript">
// Wait till OM campaign is ready.
document.addEventListener('om.Campaign.init', function( evt ) {
// Make sure we are on the right campaign.
if ( '<?php echo $this->campaign_slug; ?>' !== evt.detail.Campaign.id ) {
return;
}
// Setup an event listener for
document.addEventListener('om.Form.init', function( evt ) {
if ( '<?php echo $this->campaign_slug; ?>' !== evt.detail.Campaign.id ) {
return;
}
document.getElementById(evt.detail.Campaign.ns + '-field-name').value = <?php echo json_encode( $this->name ); ?>;
document.getElementById(evt.detail.Campaign.ns + '-field-email').value = <?php echo json_encode( $this->email ); ?>;
} );
evt.detail.Campaign.startShow();
} );
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment