Action uncanny-automator

automator_save_wp_form

Fires after a WPForms form submission is saved, providing access to form data and associated recipes for automation.

add_action( 'automator_save_wp_form', $callback, 10, 4 );

Description

Fires after a WPForms submission is saved by Uncanny Automator. Developers can use this hook to access form fields, data, and recipes, and perform custom actions related to the form submission and automation. It's primarily for internal use within the Uncanny Automator WPForms integration.


Usage

add_action( 'automator_save_wp_form', 'your_function_name', 10, 4 );

Parameters

$fields (mixed)
This parameter contains an array of field data submitted with the WPForms form.
$form_data (mixed)
This parameter contains an array of form fields that were submitted with the WPForms entry.
$recipes (mixed)
This parameter contains an array of data related to the submitted WPForms form.
$args (mixed)
This parameter contains the recipes that are associated with the WPForms submission.

Examples

<?php

/**
 * Example callback function for the 'automator_save_wp_form' action hook.
 *
 * This function demonstrates how to process saved form data and potentially
 * trigger further actions or log information.
 *
 * @param mixed $fields      The submitted form fields.
 * @param mixed $form_data   The raw data associated with the form submission.
 * @param mixed $recipes     An array of recipes that are triggered by this form.
 * @param mixed $args        Additional arguments passed to the hook, potentially including user ID and trigger entry details.
 */
function my_automator_handle_wpform_save( $fields, $form_data, $recipes, $args ) {

	// Check if we have valid data to process.
	if ( empty( $fields ) || empty( $form_data ) || empty( $args ) ) {
		return;
	}

	// Extract relevant information for logging or further processing.
	$form_id = isset( $form_data['id'] ) ? absint( $form_data['id'] ) : 0;
	$user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : 0;

	// Log the form submission details for debugging or audit purposes.
	error_log( sprintf(
		'Automator: WPForms submission saved. Form ID: %1$d, User ID: %2$d.',
		$form_id,
		$user_id
	) );

	// Example: If a specific recipe is present, perform a custom action.
	foreach ( $recipes as $recipe ) {
		if ( isset( $recipe['ID'] ) && '123' === $recipe['ID'] ) { // Replace '123' with a real recipe ID
			// Perform a custom action for this specific recipe.
			// For example, send a custom email or update a meta field.
			error_log( sprintf(
				'Automator: Special action for recipe %1$s on form submission.',
				$recipe['ID']
			) );

			// Access and process specific fields from $fields if needed.
			if ( isset( $fields['email_address'] ) ) {
				$submitted_email = sanitize_email( $fields['email_address'] );
				error_log( 'Submitted email: ' . $submitted_email );
			}
		}
	}

	// You can also save additional trigger meta here if needed for token parsing.
	// For instance, if $args contains trigger log IDs.
	if ( isset( $args['trigger_log_id'] ) ) {
		$trigger_log_id = absint( $args['trigger_log_id'] );
		// Example of saving form field data to trigger log meta
		// This would typically involve calling a function provided by Uncanny Automator
		// to store this data correctly. For demonstration, let's assume a hypothetical
		// function `automator_update_trigger_meta`.
		// automator_update_trigger_meta( $trigger_log_id, 'wpf_form_submission_data', $fields );
	}
}

// Add the action hook with the callback function.
// The priority is set to 30, and it accepts 4 arguments.
add_action( 'automator_save_wp_form', 'my_automator_handle_wpform_save', 30, 4 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/wpforms/triggers/wpf-subform.php:120
uncanny-automator-pro/src/integrations/wpforms/triggers/anon-wpf-subform-paypalpayment.php:131
uncanny-automator-pro/src/integrations/wpforms/triggers/wpf-submitfield.php:142
uncanny-automator-pro/src/integrations/wpforms/triggers/wpf-subform-paypalpayment.php:124

public function wpform_submit( $fields, $entry, $form_data, $entry_id ) {
		if ( empty( $form_data ) ) {
			return;
		}

		$user_id = get_current_user_id();
		if ( empty( $user_id ) ) {
			return;
		}

		$args = array(
			'code'    => $this->trigger_code,
			'meta'    => $this->trigger_meta,
			'post_id' => intval( $form_data['id'] ),
			'user_id' => $user_id,
		);

		$args = Automator()->process->user->maybe_add_trigger_entry( $args, false );

		// Adding an action to save form submission in trigger meta
		$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		do_action( 'automator_save_wp_form', $fields, $form_data, $recipes, $args );

		if ( $args ) {
			foreach ( $args as $r ) {
				if ( true === $r['result'] ) {
					if ( isset( $r['args'] ) && isset( $r['args']['get_trigger_id'] ) ) {
						// Saving form values in trigger log meta for token parsing!
						$wpf_args   = array(
							'trigger_id'     => (int) $r['args']['trigger_id'],
							'user_id'        => $user_id,
							'trigger_log_id' => $r['args']['get_trigger_id'],
							'run_number'     => $r['args']['run_number'],
						);
						$user_ip    = Automator()->helpers->recipe->wpforms->options->get_entry_user_ip_address( $entry_id );
						$entry_date = Automator()->helpers->recipe->wpforms->options->get_entry_entry_date( $entry_id );
						$entry_id   = Automator()->helpers->recipe->wpforms->options->get_entry_entry_id( $entry_id );

						$wpf_args['meta_key']   = 'WPFENTRYID';
						$wpf_args['meta_value'] = $entry_id;
						Automator()->insert_trigger_meta( $wpf_args );

						$wpf_args['meta_key']   = 'WPFENTRYIP';
						$wpf_args['meta_value'] = $user_ip;
						Automator()->insert_trigger_meta( $wpf_args );

						$wpf_args['meta_key']   = 'WPFENTRYDATE';
						$wpf_args['meta_value'] = maybe_serialize( Automator()->helpers->recipe->wpforms->options->get_entry_date( $entry_date ) );
						Automator()->insert_trigger_meta( $wpf_args );
					}
					Automator()->process->user->maybe_trigger_complete( $r['args'] );
				}
			}
		}
	}

Internal Usage

Found in src/integrations/wpforms/tokens/wpf-tokens.php:25:

add_action( 'automator_save_wp_form', array( $this, 'wpf_form_save_entry' ), 40, 4 );
Scroll to Top