Custom Action

Actions play a vital role in automated funnels. Actions are executed after any benchmarks are completed by a contact and performs series of steps on server side to move forward with an automated process. But sometimes you need to do something a little special. So we’ve made creating your own custom actions super easy!

If you would like to create your own Funnel Actions follow these steps. We are going to give the example of adding tags to a contact.

1. The Funnel Step Class

To start building your own action we will define a class call My_Action. This class will extend the WPGH_Funnel_Step class.

The WPGH_Funnel_Step class does a LOT of the heavy lifting for you as far as hooking in to the rest of Groundhogg and WordPress, extending it will make our job a LOT easier.

class My_Action extends WPGH_Funnel_Step
{
    //todo 
}

2. Setting Up Your Action

In the __construct method you need to define which type of step it is, the unique step identifier, pretty step name and a url to an icon to use in the funnel builder Action panel.

public function __construct()
{
    # Give your action a custom identifier. NOTE: Your identifier must be less than 20 characters in length.
    $this->type     = 'custom_action';

    # You need to define that this is in fact a action.
    $this->group    = 'action';

    # This is the name of the benchmark as seen from the funnel builder panel.
    $this->name     = __( 'My Action' );

    # Define a url to the image you'd like to use as an Icon. Square images are reccomended.
    $this->icon     = 'url_to_my_icon.png' ;

    # you MUST call the parent __construct method as well.
    parent::__construct();
}

At this point, if you where to add the action to your project, you’d see the icon show up in the actions panel.

3. Add Your Settings

An Action would be pretty useless unless it had some settings for you to define what happens.

We are going to add settings which will add some tags to a contact.

The settings method accepts 1 parameter which is a WPGH_Step object. This particular class has a LOT of helper methods that will allow you to define your custom settings.

/**
 * @param $step WPGH_Step the step object with helper methods.
 */
public function settings( $step )
{

     $tags = $step->get_meta( 'tags' );
     if ( ! $tags )
            $tags = array();
     ?>
     <table class="form-table">
     <tr>
         <th><?php _e( 'Apply These Tags' ); ?></th>
         <td><?php
              $args = array(
                  # use the $step->prefix helper method to uniquely identify settings in the funnel builder.
                'id'       => $step->prefix( 'tags' ),
                'name'     => $step->prefix( 'tags' ) . '[]',
                'selected' => $tags
              );

              # use the WPGH()->html helper class to create a tag picker quickly.
               echo WPGH()->html->tag_picker( $args ); ?>
         </td>
     </tr>
     </table>
     <?php
}

4. Save Your Settings

Now that you’ve displayed your settings you should save them so that they can be used to perform Actions.

/**
 * @param $step WPGH_Step the step object with helper methods.
 */
public function save ( $step )
{
    # our tags were displayed using the $step->prefix() method, so we access them in the $_POST using the same method.

    if ( isset( $_POST[ $step->prefix( 'tags' ) ] ) ){
         # here we use a special function to validate tag input. It converts text into new tags and provides an array of Ids at the end.
         $tags = WPGH()->tags->validate( $_POST[ $step->prefix( 'tags' ) ] );
         $step->update_meta( 'tags', $tags );
    }
}

5. Run Actions

Now that we have our settings, we can implement the Run method which will executes as a part of action when a contact reaches it in the funnel.


/**
* @param $contact WPGH_Contact the contact class object with helper methods. 
* @param $event WPGH_Event the event class object with helper methods.     * @return true|false based on status of action.
*/
public function run( $contact, $event )
{
    # the WPGH_Event class has the 'step' property which is a WPGH_Step object.
    #retrieves the value of tags from the meta table
    $tags = $event->step->get_meta( 'tags' );

    # Adds the tags to the contact and return true/false
    return $contact->add_tag( $tags );
}

Note that if your run() method returns false, the contact will NOT move forward in the funnel.

6. Putting It All Together

class My_Action extends WPGH_Funnel_Step
{  

    public function __construct()
    {
        # Give your action a custom identifier. NOTE: Your identifier must be less than 20 characters in length.
        $this->type     = 'custom_action';

        # You need to define that this is in fact a action.
        $this->group    = 'action';

        # This is the name of the benchmark as seen from the funnel builder panel.
        $this->name     = __( 'My Action' );

        # Define a url to the image you'd like to use as an Icon. Square images are reccomended.
        $this->icon     = 'url_to_my_icon.png' ;

        # you MUST call the parent __construct method as well.
        parent::__construct();
    }

    /**
     * @param $step WPGH_Step the step object with helper methods.
     */
    public function settings( $step )
    {    
        $tags = $step->get_meta( 'tags' );
        if ( ! $tags )
            $tags = array();
         ?>
         <table class="form-table">
         <tr>
             <th><?php _e( 'Apply These Tags :' ); ?></th>
             <td><?php
                  $args = array(
                      # use the $step->prefix helper method to uniquely identify tags in the funnel builder.
                     'id'       => $step->prefix( 'tags' ),
                     'name'     => $step->prefix( 'tags' ) . '[]',
                     'selected' => $tags
                  );

                  # use the WPGH()->html helper class to create a tag quickly.
                  echo WPGH()->html->tag_picker( $args ); ?>
             </td>
         </tr>
         </table>
         <?php
     }

    /**
     * @param $step WPGH_Step the step object with helper methods.
     */
    public function save ( $step )
    {
        # our settings were displayed using the $step->prefix() method, so we access them in the $_POST using the same method.

        if ( isset( $_POST[ $step->prefix( 'tags' ) ] ) ){
            $tags = WPGH()->tags->validate( $_POST[ $step->prefix( 'tags' ) ] );
            $step->update_meta( 'tags', $tags );
        }
    }

    /**
    * @param $contact WPGH_Contact the contact class object with helper     methods. 
    * @param $event WPGH_Event the event class object with helper methods.    
    * @return true|false based on status of operation.
    */
    public function run( $contact, $event )
    {
        #retrieves the value of store tag form the database 
        $tags = $event->step->get_meta( 'tags' );

        #Adds single tag or list of tag to contact.
        return $contact->add_tag( $tags );
    }
}

# Somewhere else in your code
$my_action = new My_Action();

Implement all the methods with your own custom code and you are ready to launch your own new custom action for Groundhogg.

Was this article helpful to you? Yes No

How can we help?