Converting Contact Form 7 Entries into Subscribers

Updated on Apr 28, 2024

If you wish to store your client’s contact information in the WP SMS newsletter using contact form 7, the following function will assist you in doing so easily.

/**
 * @param WPCF7_ContactForm $WPCF7_ContactForm
 */
function addNumberToSubscriberList($WPCF7_ContactForm)
{
    $name   = isset($_POST['your-name']) ? sanitize_text_field($_POST['your-name']) : "";
    $mobile = isset($_POST['your-mobile']) ? sanitize_text_field($_POST['your-mobile']) : "";
    if ($name and $mobile) {
        \WP_SMS\Newsletter::addSubscriber($name, $mobile, 'group-id');
    }
}
add_action('wpcf7_before_send_mail', 'addNumberToSubscriberList');

In order to display the list of groups on your form, you must first create a dropdown field in Contact Form 7 that includes the groups.

[select group "1|News" "2|Sport" "3|General"]

Then you can access the group ID by $_POST['group']

**
 * @param WPCF7_ContactForm $WPCF7_ContactForm
 */
function addNumberToSubscriberList($WPCF7_ContactForm)
{
    $name   = isset($_POST['your-name']) ? sanitize_text_field($_POST['your-name']) : "";
    $mobile = isset($_POST['mobile-number']) ? sanitize_text_field($_POST['mobile-number']) : "";
    $group = isset($_POST['group']) ? sanitize_text_field($_POST['group']) : "";
    if ($name and $mobile) {
        \WP_SMS\Newsletter::addSubscriber($name, $mobile, $group);
    }
}
add_action('wpcf7_before_send_mail', 'addNumberToSubscriberList');