Send SMS from Frontend

Allow website visitors to send SMS messages through a frontend form.

Allow website visitors to send SMS messages through your WordPress site using a custom shortcode.

Create the Shortcode

Add this code to your theme’s functions.php file:

add_shortcode('wpsms_send_form', function () {
    ?>
    <form method="post">
        <?php wp_nonce_field('send_sms_nonce', 'wpsms_nonce'); ?>
        <textarea name="content" placeholder="Enter your message"></textarea>
        <button type="submit">Send SMS!</button>
    </form>
    <?php
    if (isset($_POST['wpsms_nonce']) && wp_verify_nonce($_POST['wpsms_nonce'], 'send_sms_nonce')) {
        $adminMobileNumber[] = \WP_SMS\Option::getOption('admin_mobile_number');
        wp_sms_send($adminMobileNumber, sanitize_text_field($_POST['content']));
        wp_safe_redirect(wp_get_referer());
        exit;
    }
});

Usage

Add the shortcode to any page or post:

[wpsms_send_form]

How It Works

  1. Form Display - Shows a textarea and submit button
  2. Security - Uses WordPress nonce for CSRF protection
  3. Sanitization - Message content is sanitized with sanitize_text_field()
  4. Recipient - Sends to the admin mobile number configured in WP SMS settings
  5. Redirect - Returns user to the same page after sending

Customization

Custom Recipient

To send to a specific number instead of admin:

$recipient = ['+12025550191'];
wp_sms_send($recipient, sanitize_text_field($_POST['content']));

Add Phone Number Field

Allow users to specify the recipient:

add_shortcode('wpsms_send_form', function () {
    ?>
    <form method="post">
        <?php wp_nonce_field('send_sms_nonce', 'wpsms_nonce'); ?>
        <input type="tel" name="phone" placeholder="Phone number" required>
        <textarea name="content" placeholder="Enter your message" required></textarea>
        <button type="submit">Send SMS!</button>
    </form>
    <?php
    if (isset($_POST['wpsms_nonce']) && wp_verify_nonce($_POST['wpsms_nonce'], 'send_sms_nonce')) {
        $recipient = [sanitize_text_field($_POST['phone'])];
        wp_sms_send($recipient, sanitize_text_field($_POST['content']));
        wp_safe_redirect(wp_get_referer());
        exit;
    }
});

Last updated: December 28, 2025