Automated Birthday SMS Notifications in WordPress Using WP SMS

By engaging with your users personally, you can significantly improve their experience and loyalty. Sending automated birthday SMS notifications is one way to accomplish this. WordPress users can easily implement this feature with the WP SMS plugin. Using this guide, you will learn how to set up a birthday field, configure settings, and automate SMS notifications.

Why Use WP SMS for Birthday Notifications?

With WP SMS, you can integrate SMS capabilities directly into your WordPress website. With it, you can communicate directly with your users via SMS gateways. You can strengthen your relationship with your audience by sending automated birthday wishes.

Step 1: Registering a Birthday and Mobile Number Field in WordPress

To begin with, you need to collect your users’ birthdays and mobile phone numbers. This information can be gathered through their user profiles.

Adding the Birthday Field

Add the following PHP code to your theme’s functions.php file or a custom plugin to create a birthday field:

<?php
function add_birthday_field($user)
{?>
    <h3><?php _e("Additional Profile Information", "your-text-domain"); ?></h3>
    <table class="form-table">
    <tr>
        <th>
         <label for="birthday"><?php _e("Birthday", "your-text-domain"); ?></label>
        <th>
        <td>
            <input type="date" name="birthday" id="birthday" value="<?php echo esc_attr(get_the_author_meta('birthday', $user->ID)); ?>" class="regular-text"/>
            <p class="description"><?php _e("Please enter your birthday.", "your-text-domain"); ?></p>
        </td>
    </tr>
    </table><?php
}

function save_birthday_field($user_id)
{
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }
    update_user_meta($user_id, 'birthday', $_POST['birthday']);
}

add_action('show_user_profile', 'add_birthday_field');
add_action('edit_user_profile', 'add_birthday_field');
add_action('personal_options_update', 'save_birthday_field');
add_action('edit_user_profile_update', 'save_birthday_field');

Adding the Mobile Number Field

Enable the “Insert a mobile number field into user profiles” option by navigating to WP SMS → Settings → General → Mobile Field Configuration. This setting automatically adds a field to user profiles to store mobile numbers, which WP SMS will use to send SMS notifications.

Step 2: Setting a Daily Schedule to Send SMS

The next step is to create a daily WordPress cron job to check if any user’s birthday matches the current date, then send an SMS.

<?php
function send_birthday_sms_daily()
{
    $today = date('n-d');
    $args = array('meta_query' => array(array('key' => 'birthday', 'value' => $today, 'compare' => 'LIKE')));
    $users_with_birthday_today = get_users($args);

    foreach ($users_with_birthday_today as $user) {
        $user_mobile = \WP_SMS\Helper::getUserMobileNumberByUserId($user->ID);
        if (!empty($user_mobile)) {
            wp_sms_send($user_mobile, 'Happy Birthday! Hope you have a fantastic day!');
        }
    }
}

if (!wp_next_scheduled('send_birthday_sms_daily_hook')) {
    $next_six_am = strtotime('tomorrow 6am');
    wp_schedule_event($next_six_am, 'daily', 'send_birthday_sms_daily_hook');
}

// Hook the function to the scheduled action
add_action('send_birthday_sms_daily_hook', 'send_birthday_sms_daily');

This script checks each user’s birthday and uses the \WP_SMS\Helper::getUserMobileNumberByUserId($user_id) function to retrieve their mobile number to send the SMS.

Step 3: Ensure Proper Configuration of WP SMS

Ensure your WP SMS plugin is configured correctly with your preferred SMS gateway. SMS messages must be configured correctly in order for them to be delivered without problems.

You can engage your users by setting up automated birthday SMS notifications in WordPress using WP SMS. As a result, they feel appreciated and receive personal attention. To ensure everything works on your live site, make sure you thoroughly test the functionality.