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
- Form Display - Shows a textarea and submit button
- Security - Uses WordPress nonce for CSRF protection
- Sanitization - Message content is sanitized with
sanitize_text_field() - Recipient - Sends to the admin mobile number configured in WP SMS settings
- 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;
}
});
Related
- wp_sms_send - Programmatic SMS sending
- How to Send SMS - Send SMS from admin
- Quick Reply - Quick reply in admin dashboard
Last updated: December 28, 2025