Last updated on October 18, 2023

WP Forms form status tracking

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Track the status of form submissions in WP Forms.

To track the status of a WP Forms form submission, you can use the wpforms_entry_update hook provided by the WP Forms plugin. This hook allows you to perform custom actions when a form entry is updated.

Here’s an example code snippet that demonstrates how to track the status of a WP Forms form submission:

/**
 * Update form status after submission.
 *
 * @param array $entry The form entry data.
 * @param int   $form_id The ID of the form.
 */
function wpsnippets_update_form_status( $entry, $form_id ) {
    // Get the status field value from the form entry.
    $status = $entry['fields'][0]['value'];

    // Perform your custom actions based on the form status.
    if ( $status === 'completed' ) {
        // Do something when the form status is 'completed'.
    } elseif ( $status === 'pending' ) {
        // Do something when the form status is 'pending'.
    } elseif ( $status === 'rejected' ) {
        // Do something when the form status is 'rejected'.
    }
}
add_action( 'wpforms_entry_update', 'wpsnippets_update_form_status', 10, 2 );

In this example, we define a custom function wpsnippets_update_form_status that accepts two parameters: $entry (the form entry data) and $form_id (the ID of the form). Inside the function, we retrieve the value of the status field from the form entry using the $entry parameter.

You can then perform your custom actions based on the form status. In this example, we check the value of the status field and perform different actions for each status: ‘completed’, ‘pending’, and ‘rejected’. You can modify the code to suit your specific needs.

Finally, we use the add_action function to hook our custom function to the wpforms_entry_update action. This ensures that our function is called whenever a form entry is updated.

By using this code snippet, you can easily track the status of WP Forms form submissions and perform custom actions based on the status.

Examples

Example 1: Tracking form submissions and displaying status on the front-end

This use case involves tracking the status of form submissions made through WP Forms and displaying the status on the front-end of your WordPress website. The code example below demonstrates how to track the form submissions and display the status using a custom shortcode.

/**
 * Tracks form submissions and displays status on the front-end.
 */
function wpsnippets_track_form_submissions( $atts ) {
    // Get the form ID from the shortcode attributes
    $form_id = $atts['id'];

    // Get the total number of form submissions
    $total_submissions = wpsnippets_get_total_form_submissions( $form_id );

    // Get the last submission status
    $last_submission_status = wpsnippets_get_last_submission_status( $form_id );

    // Display the form status
    $output = 'Total Submissions: ' . $total_submissions . '<br>';
    $output .= 'Last Submission Status: ' . $last_submission_status;

    return $output;
}
add_shortcode( 'form_status', 'wpsnippets_track_form_submissions' );

In this code example, we define a custom shortcode form_status that accepts an id attribute representing the form ID. The shortcode function retrieves the total number of form submissions and the status of the last submission using the wpsnippets_get_total_form_submissions() and wpsnippets_get_last_submission_status() functions respectively. The function then returns the form status as an output.

Example 2: Sending email notifications for form status updates

This use case involves sending email notifications whenever the status of a form submission changes. The code example below demonstrates how to achieve this by hooking into the WP Forms submission process.

/**
 * Sends email notifications for form status updates.
 */
function wpsnippets_send_form_status_email( $entry, $form_data ) {
    // Get the form ID from the submitted entry
    $form_id = $form_data['id'];

    // Get the previous submission status
    $previous_status = wpsnippets_get_last_submission_status( $form_id );

    // Get the current submission status
    $current_status = $entry['status'];

    // Check if the status has changed
    if ( $previous_status !== $current_status ) {
        // Send email notification
        $to = 'admin@example.com';
        $subject = 'Form Status Update';
        $message = 'The status of the form submission has changed to: ' . $current_status;
        wp_mail( $to, $subject, $message );
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_status_email', 10, 2 );

In this code example, we hook into the wpforms_process_complete action to execute the wpsnippets_send_form_status_email() function whenever a form submission is processed. The function retrieves the previous and current submission status using the wpsnippets_get_last_submission_status() function and compares them. If the status has changed, an email notification is sent to the admin with the updated status.

Example 3: Displaying form status in the WordPress admin dashboard

This use case involves displaying the form status in the WordPress admin dashboard for easy monitoring. The code example below demonstrates how to add a custom column to the WP Forms submissions list table and populate it with the form status.

/**
 * Adds a custom column to the WP Forms submissions list table.
 */
function wpsnippets_add_form_status_column( $columns ) {
    $columns['form_status'] = 'Form Status';
    return $columns;
}
add_filter( 'wpforms_admin_entries_columns', 'wpsnippets_add_form_status_column' );

/**
 * Populates the custom column with the form status.
 */
function wpsnippets_populate_form_status_column( $value, $column_name, $entry ) {
    if ( 'form_status' === $column_name ) {
        $form_id = $entry->form_id;
        $status = wpsnippets_get_submission_status( $form_id );
        return $status;
    }
    return $value;
}
add_filter( 'wpforms_admin_entries_custom_column', 'wpsnippets_populate_form_status_column', 10, 3 );

In this code example, we first add a custom column called “Form Status” to the WP Forms submissions list table by hooking into the wpforms_admin_entries_columns filter. Then, we populate the custom column with the form status by hooking into the wpforms_admin_entries_custom_column filter. The wpsnippets_get_submission_status() function retrieves the form status based on the form ID.

Last updated on October 18, 2023. Originally posted on December 25, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *