
Manually copying form submissions into your CRM or marketing tools wastes hours of valuable business time. When your team misses a lead or delays a customer response, you lose revenue and damage trust. Fortunately, you do not have to handle these repetitive tasks by hand. The gform_after_submission hook allows you to automate your workflows right after a user clicks submit.
By using this versatile gravity forms php hook, you can connect your forms to external tools, register users, or trigger custom notifications instantly. In this guide, we will break down exactly how the gravity forms after submission hook works. You will get 10 real-world, copy-and-paste PHP examples to streamline your operations and eliminate manual data entry. These solutions will help you regain control of your site’s data flow in 2026.
What Is the gform_after_submission Hook?
The gform_after_submission hook is a WordPress action hook provided by Gravity Forms. It triggers immediately after a user submits a form, and after Gravity Forms saves the entry to your database. This makes it the perfect place to run custom PHP code that relies on finalized submission data.
Unlike hooks that run before submission, this hook executes after the entry is created, notifications are sent, and confirmations are processed. It ensures that your custom code only runs for successful submissions. If a payment fails or a validation error occurs, this hook will not trigger, which prevents duplicate or incorrect data processing.
Using this hook allows you to extend your website’s functionality far beyond basic form collection. You can pass data to external APIs, update user roles, or sync submission details with custom database tables. It bridges the gap between your front-end forms and your back-end business systems.
How Does the Gravity Forms After Submission Hook Work?
The gravity forms after submission hook passes two essential parameters to your custom PHP function. These parameters are the $entry array and the $form array. Understanding these two variables is key to writing successful custom functions.
The $entry array contains all the submitted data, including field values, submission time, user IP address, and entry ID. The $form array contains the structure and configuration of the form itself, such as field labels, form settings, and active add-ons. You can target all forms on your site or target a specific form by appending the form ID to the hook name.
Here is the basic structure of the hook targeted to a specific form:
add_action( 'gform_after_submission_5', 'my_custom_submission_handler', 10, 2 );
function my_custom_submission_handler( $entry, $form ) {
// Your custom code goes here
}By targeting form ID 5, you ensure this code only runs when that specific form is submitted. This keeps your site fast and prevents unnecessary code execution on other pages.
Integrating Marketing and CRM Platforms
Automating your lead capture process ensures that your sales team can follow up with prospects instantly. Let’s look at two practical examples of how to connect your forms directly to your marketing platforms.
Example 1: Send Lead Data to a Custom CRM API
You can use the gravity forms php hook to send submitted lead details to an external CRM. This example uses WP_Query and wp_remote_post to send a secure POST request to your CRM’s API endpoint.
add_action( 'gform_after_submission_1', 'send_lead_to_crm', 10, 2 );
function send_lead_to_crm( $entry, $form ) {
$api_url = 'https://api.yourcrm.com/v1/leads';
$body = array(
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'email' => rgar( $entry, '2' ),
'phone' => rgar( $entry, '3' ),
);
wp_remote_post( $api_url, array(
'method' => 'POST',
'body' => json_encode( $body ),
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_KEY',
),
));
}Example 2: Auto-Tag Mailchimp Subscribers Based on Form Inputs
This code checks a dropdown selection on your form and applies a specific tag to the user in Mailchimp. This helps segment your email list based on user preferences.
add_action( 'gform_after_submission_2', 'tag_mailchimp_subscriber', 10, 2 );
function tag_mailchimp_subscriber( $entry, $form ) {
$interest = rgar( $entry, '4' ); // Assuming field 4 is the dropdown
$email = rgar( $entry, '2' );
// Code to call Mailchimp API and add tag based on $interest value
// This ensures targeted email campaigns based on user selection
}Enhancing User Management and WooCommerce
Managing WordPress users and e-commerce customers manually is time-consuming. You can automate user registration and profile updates directly from your form submissions.
Example 3: Create a Custom WordPress User After Submission
This example registers a new WordPress user automatically when a specific form is submitted. It assigns a custom role and prevents registration if the email already exists.
add_action( 'gform_after_submission_3', 'create_wordpress_user', 10, 2 );
function create_wordpress_user( $entry, $form ) {
$email = rgar( $entry, '2' );
$username = rgar( $entry, '5' );
$password = wp_generate_password();
if ( ! email_exists( $email ) && ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email );
wp_update_user( array(
'ID' => $user_id,
'role' => 'subscriber',
) );
}
}Example 4: Update WooCommerce Customer Meta Data
If you run a WooCommerce store, you can update custom customer meta fields when they submit a support or feedback form.
add_action( 'gform_after_submission_4', 'update_customer_meta', 10, 2 );
function update_customer_meta( $entry, $form ) {
$user_id = get_current_user_id();
if ( $user_id ) {
$vip_status = rgar( $entry, '6' ); // Field 6 contains membership choice
update_user_meta( $user_id, 'billing_vip_status', $vip_status );
}
}Automating Notifications and Team Alerts
Keeping your team informed of new submissions is vital for fast response times. You can trigger custom SMS or Slack alerts based on specific form values.
Example 5: Send a Custom SMS Notification via Twilio
This code triggers a text message alert to your sales manager when a high-value lead submits a request on your website.
add_action( 'gform_after_submission_5', 'send_sms_via_twilio', 10, 2 );
function send_sms_via_twilio( $entry, $form ) {
$phone = rgar( $entry, '3' );
$message = 'New high-priority lead submitted: ' . rgar( $entry, '1.3' );
// Twilio API details
$sid = 'YOUR_TWILIO_SID';
$token = 'YOUR_TWILIO_TOKEN';
$url = "https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json";
wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "$sid:$token" ),
),
'body' => array(
'From' => '+1234567890',
'To' => '+1987654321',
'Body' => $message,
),
));
}Example 6: Trigger a Slack Notification for High-Priority Leads
You can post submission details directly to your company’s Slack channel to keep your sales and support teams aligned.
add_action( 'gform_after_submission_6', 'send_slack_notification', 10, 2 );
function send_slack_notification( $entry, $form ) {
$webhook_url = 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL';
$name = rgar( $entry, '1.3' );
$payload = array(
'text' => "New lead received from: $name",
);
wp_remote_post( $webhook_url, array(
'body' => json_encode( $payload ),
'headers' => array( 'Content-Type' => 'application/json' ),
));
}Handling Files and Custom Data Operations
Sometimes you need to move beyond standard WordPress fields and handle complex data structures or custom tables.
Example 7: Log Form Submissions to a Custom Database Table
This code writes submission details to a custom database table for advanced reporting or auditing purposes.
add_action( 'gform_after_submission_7', 'log_to_custom_table', 10, 2 );
function log_to_custom_table( $entry, $form ) {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_form_logs';
$wpdb->insert(
$table_name,
array(
'entry_id' => $entry['id'],
'user_email' => rgar( $entry, '2' ),
'submitted' => current_time( 'mysql' ),
)
);
}Example 8: Generate a Custom PDF Receipt and Email It
This snippet hooks into the submission to prepare variables for a PDF generation library, ensuring customers receive a professional receipt.
add_action( 'gform_after_submission_8', 'generate_pdf_receipt', 10, 2 );
function generate_pdf_receipt( $entry, $form ) {
$customer_name = rgar( $entry, '1.3' );
$amount = rgar( $entry, '10' );
// Code to call PDF library (like Dompdf or FPDF) goes here
// Send generated PDF as an attachment via wp_mail
}Optimising Site Performance and External APIs
You can also use form submissions to trigger site actions that improve your website performance or connect to automation tools like Zapier.
Example 9: Clear Site Cache Automatically After a Survey Submission
If your form submissions update public-facing data on your site, you may need to clear your cache plugin automatically to show the fresh results.
add_action( 'gform_after_submission_9', 'clear_cache_on_submission', 10, 2 );
function clear_cache_on_submission( $entry, $form ) {
if ( function_exists( 'rocket_clean_domain' ) ) {
rocket_clean_domain(); // Clears WP Rocket cache
}
}Example 10: Send Data to a Webhook URL
This code sends a payload to a webhook URL, allowing you to trigger custom workflows on platforms like Make.com or Zapier without paying for premium add-ons.
add_action( 'gform_after_submission_10', 'send_to_webhook', 10, 2 );
function send_to_webhook( $entry, $form ) {
$webhook_url = 'https://make.yourdomain.com/webhook-receiver';
wp_remote_post( $webhook_url, array(
'body' => json_encode( $entry ),
'headers' => array( 'Content-Type' => 'application/json' ),
));
}Why Should You Avoid Adding Custom PHP Directly to Your Theme?
Adding custom PHP code directly to your parent theme’s functions.php file is a major security and stability risk. When your theme receives updates, all your custom code modifications will be completely overwritten. This breaks your integrations and causes immediate site downtime.
According to a security report from Wordfence, vulnerability exploits in WordPress plugins and poorly configured custom code account for the vast majority of successful site intrusions. Placing your custom hooks in a child theme or a custom functionality plugin keeps your code separate from core theme files.
Additionally, incorrect code syntax can trigger critical PHP errors that lock you out of your WordPress dashboard. Managing these changes requires careful plugin management and professional oversight. If you want to avoid these technical headaches, investing in a monthly WordPress maintenance service ensures your custom code remains secure, active, and fully functional.
How Can You Safely Maintain Custom Gravity Forms Code?
Maintaining custom code requires a structured approach to website health checks and regular testing. You should never write or paste custom PHP code directly onto a live production website. Instead, always test your changes in a staging environment first.
Before implementing any custom gravity forms php hook, verify that you have a reliable WordPress backup in place. Automatic backups give you a safety net if a plugin update or code conflict crashes your site. You must also set up uptime monitoring to alert you immediately if a code modification causes site downtime.
Keeping your WordPress core, themes, and plugins updated is crucial to prevent plugin conflicts. Custom code that works today might break after a major Gravity Forms or WordPress core update. If managing these technical details feels overwhelming, outsourcing your site’s upkeep to experts is the best path forward.
Frequently Asked Questions
Q: What is the difference between gform_after_submission and gform_pre_submission?
A: The gform_pre_submission hook runs before the entry is saved to the database, allowing you to modify values. The gform_after_submission hook runs after the entry is saved and notifications are sent, making it ideal for sending data to external APIs. If you need to manipulate input data before it is stored, use the pre-submission hook instead.
Q: Can I target a specific Gravity Form with this hook?
A: Yes, you can target a specific form by appending the form ID to the hook name, like gform_after_submission_5 for form ID 5. This prevents your code from running on every form on your site, which improves website performance. It is always best practice to target specific forms to avoid conflicts.
Q: Will custom PHP code in my theme break during WordPress updates?
A: Yes, if you add custom PHP code directly to your parent theme’s functions.php file, it will be overwritten during theme updates. To prevent this, use a child theme or a custom functionality plugin, or hire managed WordPress support to manage your code safely. This keeps your custom workflows active and secure.
Q: How do I access specific form field values inside the hook?
A: You can access field values using the $entry array and the field ID. For example, $entry[‘3’] or the helper function rgar( $entry, ‘3’ ) will retrieve the submitted value for the form field with ID 3. This allows you to map specific form fields to your custom external APIs.
Q: What should I do if a custom PHP hook causes a critical site error?
A: If a custom PHP hook crashes your site, you should immediately access your files via FTP or your hosting file manager and temporarily comment out the code. Keeping a recent WordPress backup ensures you can restore your website performance quickly if things go wrong. For ongoing peace of mind, consider a dedicated care plan to handle these issues for you.
Conclusion
Automating your Gravity Forms workflows with the gform_after_submission hook is a powerful way to eliminate manual data entry and connect your website to your essential business tools. Whether you are sending leads to a custom CRM, registering new WordPress users, or sending instant SMS alerts, this hook makes it possible. However, writing and maintaining custom PHP code requires careful handling to prevent security vulnerabilities and plugin conflicts.
To keep your integrations running smoothly without risking site downtime, your code needs regular testing, monitoring, and updates. If you want to focus on growing your business instead of troubleshooting PHP syntax errors, let our experts handle the technical heavy lifting. Explore our professional WordPress care plan today to keep your website fast, secure, and perfectly automated.
Zeeshan is a seasoned web developer with over 8+ years of experience, specializing in WordPress, Themosis, and Laravel. customized web solutions. Through his website, zeeshanwebexpert.com, Zeeshan offers professional web services, ensuring long-term solutions for clients.


