Last updated on October 18, 2023

WP Forms push notifications

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

Send push notifications for form responses.

To enable push notifications for WP Forms in WordPress, you can use the wpforms_process_complete action hook along with the wp_mail() function to send an email notification whenever a form is submitted. Here’s an example code snippet that demonstrates this:

function wpsnippets_wpforms_push_notifications( $fields, $entry, $form_data ) {
    // Get the form ID
    $form_id = $form_data['id'];

    // Get the form title
    $form_title = $form_data['settings']['form_title'];

    // Get the submitted data
    $name = $fields['name'];
    $email = $fields['email'];
    $message = $fields['message'];

    // Prepare the email content
    $subject = 'New Form Submission: ' . $form_title;
    $body = "A new form submission has been received for the form: $form_titlenn";
    $body .= "Name: $namen";
    $body .= "Email: $emailn";
    $body .= "Message: $messagen";

    // Set the email recipient(s)
    $to = 'admin@example.com';

    // Send the email
    wp_mail( $to, $subject, $body );
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_push_notifications', 10, 3 );

In this code snippet, we define a custom function wpsnippets_wpforms_push_notifications that hooks into the wpforms_process_complete action. This action is triggered when a WP Form is successfully submitted. The function receives three parameters: $fields, $entry, and $form_data. We use these parameters to extract the relevant form data, such as the form ID, form title, and submitted fields.

Next, we prepare the email content by concatenating the form data into a string. We set the subject of the email to include the form title and define the recipient(s) of the email. Finally, we use the wp_mail() function to send the email.

By adding this code snippet to your WordPress theme’s functions.php file or a custom plugin, you can receive push notifications via email whenever a WP Form is submitted. Remember to customize the email recipient(s) and modify the email content as per your requirements.

Examples

Example 1: Sending Push Notifications for WP Forms Submissions

This use case demonstrates how to send push notifications when a user submits a form created with WP Forms plugin.

function wpsnippets_send_push_notification( $entry, $form_data ) {
    // Get the form ID
    $form_id = $form_data['id'];

    // Get the form title
    $form_title = $form_data['settings']['form_title'];

    // Get the submitted data
    $submitted_data = $entry['fields'];

    // Prepare the push notification message
    $message = 'New form submission for ' . $form_title . ':';

    foreach ( $submitted_data as $field ) {
        $message .= "n" . $field['label'] . ': ' . $field['value'];
    }

    // Send the push notification
    // Code to send push notification goes here
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_push_notification', 10, 2 );

This code example demonstrates how to send push notifications when a user submits a form created with WP Forms plugin. The wpsnippets_send_push_notification function is hooked to the wpforms_process_complete action, which is triggered after a form submission is successfully processed. Inside the function, the form ID and title are retrieved from the $form_data parameter, and the submitted form data is retrieved from the $entry parameter. The push notification message is then constructed using the form title and submitted data, and the push notification is sent using the appropriate push notification service.

Example 2: Sending Push Notifications to Specific Users

This use case demonstrates how to send push notifications to specific users when a form is submitted.

function wpsnippets_send_push_notification( $entry, $form_data ) {
    // Get the user ID of the form submitter
    $user_id = get_current_user_id();

    // Check if the user has enabled push notifications
    if ( wpsnippets_is_push_notification_enabled( $user_id ) ) {
        // Prepare and send the push notification
        // Code to send push notification goes here
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_push_notification', 10, 2 );

This code example demonstrates how to send push notifications to specific users when a form is submitted. The wpsnippets_send_push_notification function is hooked to the wpforms_process_complete action, which is triggered after a form submission is successfully processed. Inside the function, the user ID of the form submitter is retrieved using the get_current_user_id function. Then, the wpsnippets_is_push_notification_enabled function is used to check if the user has enabled push notifications. If push notifications are enabled, the push notification is prepared and sent using the appropriate push notification service.

Example 3: Customizing Push Notification Content

This use case demonstrates how to customize the content of push notifications sent for form submissions.

function wpsnippets_send_push_notification( $entry, $form_data ) {
    // Get the form ID
    $form_id = $form_data['id'];

    // Get the form title
    $form_title = $form_data['settings']['form_title'];

    // Get the submitted data
    $submitted_data = $entry['fields'];

    // Prepare the push notification message
    $message = 'New form submission for ' . $form_title . ':';

    foreach ( $submitted_data as $field ) {
        // Exclude certain fields from the push notification
        if ( $field['id'] !== 'exclude_field_id' ) {
            $message .= "n" . $field['label'] . ': ' . $field['value'];
        }
    }

    // Send the push notification
    // Code to send push notification goes here
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_push_notification', 10, 2 );

This code example demonstrates how to customize the content of push notifications sent for form submissions. Inside the wpsnippets_send_push_notification function, the form ID and title are retrieved from the $form_data parameter, and the submitted form data is retrieved from the $entry parameter. The push notification message is then constructed using the form title and submitted data. In this example, certain fields can be excluded from the push notification by checking their field ID and skipping them in the loop. Finally, the push notification is sent using the appropriate push notification service.

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

Leave a Reply

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