Action uncanny-automator-pro

gform_post_add_entry

Fires after a Gravity Form entry is successfully added to the database.

add_action( 'gform_post_add_entry', $callback, 10, 2 );

Description

Fires after a Gravity Form entry has been successfully created and saved to the database. Developers can use this hook to perform actions immediately following entry creation, such as updating custom data, triggering notifications, or integrating with other services. It receives the created entry object and the form object as arguments.


Usage

add_action( 'gform_post_add_entry', 'your_function_name', 10, 2 );

Parameters

$entry (mixed)
This parameter contains the Gravity Forms entry object that has just been created or added to the database.
$form (mixed)
This parameter contains the Gravity Forms entry object that has just been added.

Examples

/**
 * Example function to hook into the gform_post_add_entry action.
 * This function will be executed after a Gravity Forms entry has been successfully added.
 *
 * @param array $entry The entry object.
 * @param array $form  The form object.
 */
function my_gravity_forms_post_entry_action( $entry, $form ) {
	// Log the entry ID and form title to the WordPress debug log.
	// This is a common task for debugging or auditing new entries.
	error_log( 'Gravity Forms entry added. Entry ID: ' . $entry['id'] . ', Form Title: ' . $form['title'] );

	// Example: If the form is a specific "Contact Us" form, perform a custom action.
	if ( $form['id'] === 10 ) { // Assuming form ID 10 is the "Contact Us" form.
		// You could send a custom notification, update a custom post type,
		// or trigger another external service here.
		$user_email = rgar( $entry, '1.1' ); // Assuming email field is field ID 1.1
		if ( $user_email ) {
			wp_mail(
				'[email protected]',
				'New Contact Us Submission',
				'A new submission was received: ' . get_permalink( get_post( $entry['post_id'] ) ) // Link to entry if it created a post.
			);
		}
	}

	// Example: Update a custom meta field for the user if they submitted a specific form.
	// This assumes you have a user associated with the submission, perhaps via user registration.
	$user_id = rgar( $entry, 'created_by_user_id' ); // If the entry is linked to a user.
	if ( $user_id && $form['id'] === 5 ) { // Assuming form ID 5 is a user-related form.
		update_user_meta( $user_id, 'last_form_submission_date', current_time( 'mysql' ) );
	}
}

// Hook the function into the 'gform_post_add_entry' action.
// The '3' indicates the number of arguments the callback function accepts ($entry, $form, and potentially a third).
// In this case, the hook provides $entry and $form, so we specify 2.
add_action( 'gform_post_add_entry', 'my_gravity_forms_post_entry_action', 10, 2 );

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

uncanny-automator-pro/src/integrations/gravity-forms/actions/gf-createentry.php:163

public function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {

		$form_id = Automator()->parse->text( $action_data['meta'][ $this->action_meta ], $recipe_id, $user_id, $args );
		$fields  = json_decode( $action_data['meta']['GF_FIELDS'] ?? '[]' );

		$gfrom_input_values = $this->gf->helpers->format_input_values( $fields, $recipe_id, $user_id, $args );

		// Format time fields if present
		$gfrom_input_values = $this->time_handler->format_time_fields( $gfrom_input_values, $form_id );

		$gfrom_input_values['form_id']    = absint( $form_id );
		$gfrom_input_values['created_by'] = $user_id;

		$entry_id = GFAPI::add_entry( $gfrom_input_values );

		if ( is_wp_error( $entry_id ) ) {
			$error_message = $entry_id->get_error_message();
			throw new Exception( esc_html( $error_message ) );
		}

		// Get the entry and form objects
		$entry = GFAPI::get_entry( $entry_id );
		$form  = GFAPI::get_form( $entry['form_id'] );

		// Manually trigger the form submission action
		do_action( 'gform_after_submission', $entry, $form );

		// Trigger other actions if necessary (e.g., for third-party integrations)
		do_action( 'gform_entry_created', $entry, $form );
		do_action( 'gform_post_add_entry', $entry, $form );

		$this->hydrate_tokens(
			array(
				'ENTRY_ID'  => $entry_id,
				'ENTRY_URL' => $this->gf->helpers->get_entry_url( $entry_id, $form_id ),
			)
		);

		return true;
	}


Scroll to Top