Last updated on October 18, 2023

WP Forms SMS notifications

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

Receive SMS notifications for form submissions.

To enable SMS notifications for WP Forms in WordPress, you can use the wp_mail() function along with a third-party SMS gateway service. Here’s an example code snippet that demonstrates how to achieve this functionality:

/**
 * Sends SMS notifications for WP Forms submissions.
 *
 * @param array $fields The form fields submitted.
 * @param array $entry The form entry data.
 * @param array $form The form object.
 */
function wpsnippets_send_sms_notification( $fields, $entry, $form ) {
    // Get the phone number field value from the form submission
    $phone_number = $fields['phone_number'];

    // Prepare the SMS message
    $message = 'New form submission received from ' . $fields['name'] . '.';

    // Send the SMS notification using a third-party SMS gateway service
    wp_remote_post( 'https://api.sms-gateway-provider.com/send', array(
        'body' => array(
            'phone_number' => $phone_number,
            'message' => $message,
            'api_key' => 'your_sms_gateway_api_key',
        ),
    ) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_sms_notification', 10, 3 );

In this code snippet, we define a custom function wpsnippets_send_sms_notification() that gets triggered when a WP Forms submission is completed (wpforms_process_complete action). The function retrieves the submitted form fields, extracts the phone number field value, prepares the SMS message, and then sends it using the wp_remote_post() function.

Please note that you’ll need to replace 'your_sms_gateway_api_key' with the actual API key provided by your SMS gateway service. Additionally, make sure to adjust the SMS gateway URL and parameters according to the documentation of your chosen SMS gateway provider.

This code snippet can be useful when you want to receive SMS notifications for WP Forms submissions, allowing you to stay updated on form submissions even when you’re not actively checking your email.

Examples

Example 1: Sending SMS Notifications for WP Forms Submissions

This use case demonstrates how to send SMS notifications for WP Forms submissions using a custom PHP function. The code example below shows how to use the wpsnippets_send_sms_notification() function to send an SMS notification when a WP Forms submission is received.

function wpsnippets_send_sms_notification( $entry, $form_data ) {
    // Get the form fields data
    $name = $entry['fields'][0]['value'];
    $email = $entry['fields'][1]['value'];
    $message = $entry['fields'][2]['value'];

    // Compose the SMS message
    $sms_message = "New WP Forms submission:n";
    $sms_message .= "Name: $namen";
    $sms_message .= "Email: $emailn";
    $sms_message .= "Message: $message";

    // Send the SMS notification
    wp_mail( '+1234567890', 'New WP Forms Submission', $sms_message );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_sms_notification', 10, 2 );

In this code example, the wpsnippets_send_sms_notification() function is hooked to the wpforms_process_complete action, which is triggered when a WP Forms submission is completed. The function retrieves the form fields data from the $entry parameter, composes an SMS message with the submission details, and sends it using the wp_mail() function.

Example 2: Customizing SMS Content for WP Forms Submissions

This use case demonstrates how to customize the content of the SMS notification for WP Forms submissions. The code example below shows how to modify the SMS message content by adding additional form field values.

function wpsnippets_send_sms_notification( $entry, $form_data ) {
    // Get the form fields data
    $name = $entry['fields'][0]['value'];
    $email = $entry['fields'][1]['value'];
    $phone = $entry['fields'][2]['value'];
    $message = $entry['fields'][3]['value'];

    // Compose the SMS message
    $sms_message = "New WP Forms submission:n";
    $sms_message .= "Name: $namen";
    $sms_message .= "Email: $emailn";
    $sms_message .= "Phone: $phonen";
    $sms_message .= "Message: $message";

    // Send the SMS notification
    wp_mail( '+1234567890', 'New WP Forms Submission', $sms_message );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_sms_notification', 10, 2 );

In this code example, the wpsnippets_send_sms_notification() function is similar to the previous example, but it includes an additional form field for the phone number. The SMS message content is modified to include the phone number in the message body.

Example 3: Sending SMS Notifications to Multiple Recipients

This use case demonstrates how to send SMS notifications to multiple recipients for WP Forms submissions. The code example below shows how to modify the wp_mail() function to send the SMS notification to multiple phone numbers.

function wpsnippets_send_sms_notification( $entry, $form_data ) {
    // Get the form fields data
    $name = $entry['fields'][0]['value'];
    $email = $entry['fields'][1]['value'];
    $message = $entry['fields'][2]['value'];

    // Compose the SMS message
    $sms_message = "New WP Forms submission:n";
    $sms_message .= "Name: $namen";
    $sms_message .= "Email: $emailn";
    $sms_message .= "Message: $message";

    // Send the SMS notification to multiple recipients
    $recipients = array(
        '+1234567890',
        '+9876543210'
    );
    foreach ( $recipients as $recipient ) {
        wp_mail( $recipient, 'New WP Forms Submission', $sms_message );
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_sms_notification', 10, 2 );

In this code example, the wpsnippets_send_sms_notification() function is similar to the first example, but it includes an array of phone numbers in the $recipients variable. The wp_mail() function is called within a loop to send the SMS notification to each recipient in the array.

Last updated on October 18, 2023. Originally posted on October 29, 2023.

Leave a Reply

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