Custom Flow Triggers and Actions

📅 Published on

📝 Last updated

We’re currently revamping our documentation for 4.0.
See old documentation →

Documentation

»

Developer Guides

»

Custom Flow Triggers and Actions

Have special site functionality that you want to use within your flows? With the Advanced Features add-on you can use custom Triggers and Actions to connect your site to flows with just a little code!

The videos in this instruction reference the old Groundhogg interface, but they still work the same in the latest version of the plugin.

Custom Actions

Use the Plugin API action to call custom code from within a flow.

Insert the action anywhere into your flow to start.

From the step settings panel you can change the call name of the action, which is a fancy way of using WordPress’s pre-existing plugin API.

Once you’ve decided on a call name, simply copy the provided code snippet and add your custom logic.

You can use the same call name for multiple Plugin API Actions, allowing you to use the same logic across multiple flows, or multiple times in the same flow.

The Plugin API Action should always return…

  • true if the action was run successfully
  • false if the action failed, but to continue in the flow anyway
  • WP_Error if the action failed critically

You also have the option to throw an Exception and the flow will handle it as a critical failure (same as WP_Error).

Example

The following PHP snippet will change a contact’s opt-in status to confirmed when using the Plugin API Action with the call name update_optin_status_to_confirmed.

<?php

add_filter( 'update_optin_status_to_confirmed', 'update_optin_status_to_confirmed', 10, 2 );

/**
 * Updates the contact's opt-in status to confirmed.
 * 
 * @param $success bool|WP_Error 
 * @param $contact \Groundhogg\Contact
 *                                     
 * @return true|false|WP_Error true if success, false to skip, WP_Error if failed
 */
function update_optin_status_to_confirmed( $success, $contact ){
	
    // Return out if the action previously failed
    if ( ! $success || is_wp_error( $success ) ){
    	return $success;
    }
    
    // todo you code here
    $contact->change_marketing_preferences( \Groundhogg\Preferences::CONFIRMED );
    
    return true;
}

Custom Triggers

Use the Plugin API Trigger to trigger flows using custom code!

Start by inserting the Custom API Trigger at the point in your flow where you want your custom code to move the contact. If you want the flow to start when your code is called, add the trigger to the start of your flow.

Similar to the Plugin API Action, the Plugin API Trigger requires you specify a call name which you’ll use in code to trigger the flow.

Just like the Plugin API Action, you can use the same call name for multiple Plugin API Triggers!

The provided code snippet gives example usage of how to initiate the trigger, the important part is using the Groundhogg\do_plugin_api_benchmark() function!

You can call this function from anywhere in your custom code or plugin. It accepts two parameters, a callname which is what you specify in the step settings, and a contact_id or email_address.

Examples

So calling the function might look like…

<?php

// your code here...

\Groundhogg\do_plugin_api_benchmark( 'my_custom_trigger', '[email protected]' );

// more code...

If your custom code is based on using the WordPress user account you can provide the user ID to the function instead, and Groundhogg will fetch the appropriate linked contact record.


<?php

$user = wp_get_current_user(); // get's the current WP_User object

\Groundhogg\do_plugin_api_benchmark( 'my_custom_trigger', $user->ID, true ); // true tells the function to use the `user_id` field

// more code...

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.