Gravity Forms is a powerful tool that allows you to create forms and collect data from your website visitors.
One of the many features that make Gravity Forms so useful is the “gform_after_submission” action. This action allows you to perform custom actions after a form has been submitted.
In this guide, we’ll walk through the basics of using the “gform_after_submission” action, including what it is, how to use it, and some practical examples.
What is the “gform_after_submission” Action?
The “gform_after_submission” action is a hook that is triggered after a form is submitted.
It can be used to perform custom actions on the data that was submitted, such as sending an email, creating a new post, or updating a database.
How to Use the “gform_after_submission” Action
To use the “gform_after_submission” action, you’ll need to add a function to your WordPress site’s functions.php file. Here’s an example of what the code might look like:
add_action( 'gform_after_submission', 'my_custom_function', 10, 2 );
function my_custom_function( $entry, $form ) {
// Your custom code goes here
}
Let’s break down what’s happening in this code:
- The
add_action
function is used to hook into the “gform_after_submission” action. - The first parameter is the name of the action you want to hook into.
- The second parameter is the name of the function you want to call when the action is triggered.
- The third parameter is the priority of the action (in this case, 10).
- The fourth parameter is the number of arguments that the function will accept (in this case, 2).
Parameters
The “gform_after_submission” action passes two parameters to your custom function:
$entry
– an array of data that was submitted in the form.$form
– an array of data about the form that was submitted.
Examples
Here are five practical examples of how you can use the “gform_after_submission” action:
1. Send an Email Notification
Scenario: You want to send an email notification to yourself when someone submits a form on your website.
add_action( 'gform_after_submission', 'send_notification_email', 10, 2 );
function send_notification_email( $entry, $form ) {
$to = '[email protected]';
$subject = 'Form Submission Notification';
$message = 'A new form submission has been received.';
wp_mail( $to, $subject, $message );
}
This code will send an email to “[email protected]” with the subject “Form Submission Notification” and the message “A new form submission has been received.”
2. Create a New Post
Scenario: You want to create a new post on your WordPress site whenever someone submits a form.
add_action( 'gform_after_submission', 'create_new_post', 10, 2 );
function create_new_post( $entry, $form ) {
$post_data = array(
'post_title' => $entry[1],
'post_content' => $entry[2],
'post_author' => get_current_user_id(),
'post_status' => 'publish',
'post_type' => 'post'
);
$post_id = wp_insert_post( $post_data );
}
This code will create a new post with the title and content of the first two fields in the form, set the author to the current user, and publish the post.
3. Update a User Profile
Scenario: You want to update a user’s profile on your site when they submit a form.
add_action( 'gform_after_submission', 'update_user_profile', 10, 2 );
function update_user_profile( $entry, $form ) {
$user_id = get_current_user_id();
$user_data = array(
'first_name' => $entry[1],
'last_name' => $entry[2],
'user_email' => $entry[3]
);
wp_update_user( $user_data, $user_id );
}
This code will update the current user’s first name, last name, and email address with the values from the form.
4. Add User to a Mailchimp List
Scenario: You want to add someone to a Mailchimp list when they submit a form.
add_action( 'gform_after_submission', 'add_to_mailchimp_list', 10, 2 );
function add_to_mailchimp_list( $entry, $form ) {
$api_key = 'your-api-key';
$list_id = 'your-list-id';
$email = $entry[3];
$merge_vars = array(
'FNAME' => $entry[1],
'LNAME' => $entry[2]
);
$mc = new Mailchimp( $api_key );
$mc->lists->subscribe( $list_id, array( 'email' => $email ), $merge_vars );
}
This code will add the person who submitted the form to a Mailchimp list, with their first name and last name as merge fields.
5. Redirect to a Custom Thank You Page
Scenario: You want to redirect someone to a custom thank you page after they submit a form.
add_action( 'gform_after_submission', 'redirect_to_thankyou', 10, 2 );
function redirect_to_thankyou( $entry, $form ) {
$redirect_url = 'http://example.com/thank-you';
wp_redirect( $redirect_url );
exit;
}
This code will redirect someone to “http://example.com/thank-you” after they submit the form.