A form action is something that is done after an entry is submitted, updated, or deleted. Create a new form action as shown below.
Relevant hooks
Note: The hooks below are only available post Formidable 2.0.
- add_action('frm_registered_form_actions', 'add_my_action');
- add_action('frm_trigger_my_action_name_create_action', 'my_create_action_trigger', 10, 3);
- add_action('frm_trigger_my_action_name_update_action', 'my_update_action_trigger', 10, 3);
- add_action('frm_trigger_my_action_name_delete_action', 'my_delete_action_trigger', 10, 3);
- add_action('frm_trigger_my_action_name_action', 'trigger_my_action', 10, 4);
Register the action
Start by adding the frm_registered_form_actions hook to register your action.
add_action('frm_registered_form_actions', 'register_my_action'); function register_my_action( $actions ) { $actions['my_action_name'] = 'MyActionClassName'; include_once( dirname( dirname( __FILE__ ) ) . '/path-to-your-class/MyActionClassName.php'); return $actions; }
Now in the MyActionClassName.php file, you may add something like this:
<?php class MyActionClassName extends FrmFormAction { function __construct() { $action_ops = array( 'classes' => 'dashicons dashicons-format-aside', 'limit' => 99, 'active' => true, 'priority' => 50, 'event' => array( 'create', 'update' ), ); $this->FrmFormAction('my_action_name', __('My Action Name', 'formidable'), $action_ops); } /** * Get the HTML for your action settings */ function form( $form_action, $args = array() ) { extract($args); $action_control = $this; ?> <table class="form-table frm-no-margin"> <tbody> <tr> <th> <label>Template name</label> </th> <td> <input type="text" class="large-text" value="<?php echo esc_attr($form_action->post_content['template_name']); ?>" name="<?php echo $action_control->get_field_name('template_name') ?>"> </td> </tr> <tr> <th> <label>Content></label> </th> <td> <textarea class="large-text" rows="5" cols="50" name="<?php echo $action_control->get_field_name('my_content') ?>"><?php echo esc_attr($form_action->post_content['my_content']); ?></textarea> </td> </tr> </tbody> </table> // If you have scripts to include, you can include them here <?php } /** * Add the default values for your options here */ function get_defaults() { return array( 'template_name' => '', 'my_content'=> '', ); } }
You can replace 'dashicons dashicons-format-aside' with the class you want for the action icon. If you don't want to add your own icon, you can use the dashicons that are already loaded by WordPress.
At this point, you should be able to add your action to a form and save the settings correctly. If the settings are not saving correctly, please double check your code up to this point.
Perform an action
Now, you can do something after an entry is created, updated, or deleted.
On create
This hook is only fired when an entry is created:
add_action('frm_trigger_my_action_name_create_action', 'my_create_action_trigger', 10, 3); function my_create_action_trigger($action, $entry, $form) { // Do some magic }
Replace the my_action_name text with the name you have given to your action. This name is used in two other places and they must be identical. It is referenced in the first code block:
$actions['my_action_name'] = 'MyActionClassName';
and the second code block:
$this->FrmFormAction('my_action_name', __('My Action Name', 'formidable'), $action_ops);
On update
This hook is only fired when an entry is updated:
add_action('frm_trigger_my_action_name_update_action', 'my_update_action_trigger', 10, 3); function my_update_action_trigger($action, $entry, $form) { // Do some magic }
Replace the my_action_name text with the name you have given to your action.
On delete
This hook is only fired when an entry is deleted:
add_action('frm_trigger_my_action_name_delete_action', 'my_delete_action_trigger', 10, 3); function my_delete_action_trigger($action, $entry, $form) { // Do some magic }
Replace the my_action_name text with the name you have given to your action.
On create, update, or delete
This hook is especially useful if you want to do the same thing regardless of whether an entry is created or updated.
add_action('frm_trigger_my_action_name_action', 'trigger_my_action', 10, 4); function trigger_my_action( $action, $entry, $form, $event ) { $settings = $action->post_content; // Do your magic here }
Replace the my_action_name text with the name you have given to your action.