Triggers are the starting point of any automation. They represent the equivalent of a WordPress do_action which means that:

  1. they let others know when something happens;

  2. they provide the data to be used;

Based on the data objects a trigger provides, filters can be applied and actions can be executed.

Getting started

Make sure you have Thrive Automator to the latest version and running at least PHP 7.

Creating your first trigger

The first step for creating a new trigger is to create a new class that will extend the abstract class Thrive\Automator\Items\Trigger. After that, either your IDE will autocomplete the methods you will have to implement or you can check the following list:

  • abstract public static function get_id(): string - should return a unique identifier that will be used as a key in arrays. To avoid conflicts or overwrites we suggest using a prefix.

public static function get_id(): string { 
return 'wp/login-trigger';
}
  • public static function get_wp_hook(): string - should return a string containing an action that at one point will be triggered with do_action. For more information about how WordPress actions work you can have a look here.

public static function get_wp_hook(): string { 
return 'wp_login';
}
  • public static function get_hook_params_number(): int - number of parameters the action provides. This function is used when preparing an automation with the WordPress add_action function.

public static function get_hook_params_number(): int { 
return 2;
}
  • public static function get_provided_data_objects(): array - an array of Data_Object keys that this trigger provides for Action. only Actions that require those will be displayed to interact with the trigger. The keys from this array must match the id from Data_Object. The keys also must be in the same order as provided in the do_action hook, so we can match them automatically.

public static function get_provided_data_objects(): array { 
return [ User_Data::get_id() ];
}
  • public static function get_app_id(): string - the id of the app that will group all the triggers that you provide, in the admin user interface.

public static function get_app_id(): string { 
return Wordpress_App::get_id();
}
  • public static function get_name(): string - the name of the Trigger that will be displayed in the admin dropdown select.

public static function get_name(): string { 
return 'Wordpress Login';
}
  • public static function get_description(): string - a short description of the trigger. This will be displayed with a tooltip in the UI.

public static function get_description(): string { 
return 'Triggered when a user logs in';
}
  • public static function get_image(): string - an URL for the image that will be displayed in the dropdown select near the trigger name.

public static function get_image(): string { 
return 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Wordpress_Blue_logo.png/1200px-Wordpress_Blue_logo.png';
}

Other methods

  • public function process_params( $params = [] ) - when a WordPress action hook is triggered, it will provide its own parameters as seen here.
    This method works as a filter because we receive the raw data and return an array of Data_Objects. By default, we read the array of Data_Object keys the trigger provides and we create an instance for each one we find.

  • public function prepare_data( $data = [] ) - when a WordPress action hook is triggered, the data stored while setting up the automation in the editor can be filtered/structured for easy access during the execution.

  • public function get_automation_wp_hook() - when setting up listener(s), if the Trigger supports individual WordPress hooks for each individual automation, this function will retrieve the hook with a dynamic identifier.

  • public function is_top_level() - inside the editor, the triggers are grouped under a category which usually represents the application for which it was developed.
    If we require the Trigger to be outside of a category and be a standalone item, this can be set to true.

  • public function get_search_keywords() - inside the editor, in order for the Trigger to be more easily found, extra search tags can be added.

  • public function sync_trigger_data() - inside the editor, while setting up the automation, certain Trigger_Field may alter the provided data objects and some more actions may be enabled.

Registering the trigger

In order to register this trigger so it can appear in the admin area, we should use the thrive_automator_register_trigger function which receives as the only parameter, the class name.

Creating your first trigger field

In order to create your own Trigger_Field you need to extend Thrive\Automator\Items\Trigger_Field and implement the required basic methods.

Trigger_Field extends Action Field so the methods and their purpose are the same as those from Action Field.

About trigger fields

Trigger Fields are the fields used for setting up a trigger in the admin UI, those fields can be used to add more custom functionality to an automation based on the user's input.

Did this answer your question?