WordPress SMS API Integration
This guide provides comprehensive instructions for integrating SecureSMSC SMS API with WordPress plugins and themes.
Quick Start: Download our ready-to-use WordPress plugin below for instant integration, or follow the guide to build your own custom solution.
Prerequisites
- WordPress 5.0 or higher
- PHP 7.4 or higher
- Active API key from SecureSMSC
Download Ready-to-Use Plugin
WordPress SMS Gateway Plugin
Get our plug-and-play WordPress plugin that integrates seamlessly with SecureSMSC SMS API. No coding required!
Plugin Features:
- Easy installation and activation
- Simple API key configuration
- Send single SMS messages
- Send bulk SMS campaigns
- WooCommerce order notifications
- User registration SMS alerts
- WordPress hooks integration
- Shortcode support for pages/posts
- Admin dashboard for SMS management
- SMS delivery status tracking
- DLT template support
- Fully customizable
Download Plugin (ZIP)
Version 1.0.0 | Compatible with WordPress 5.0+ | Free to use
Installation Instructions:
- Download the plugin ZIP file
- Go to WordPress Admin → Plugins → Add New
- Click "Upload Plugin" and select the downloaded ZIP file
- Click "Install Now" and then "Activate Plugin"
- Go to Settings → SMS Gateway to configure your API key
- Start sending SMS messages!
Plugin Setup (Manual Development)
If you prefer to build your own custom plugin, follow the steps below:
Main Plugin File
PHP
<?php
/**
* Plugin Name: SMS Gateway Integration
* Description: Integrate SecureSMSC SMS API with WordPress
* Version: 1.0.0
* Author: Your Name
*/
if (!defined('ABSPATH')) {
exit;
}
// Define constants
define('SMS_API_BASE_URL', 'https://whitelabled.securesmsc.com/api/v2');
define('SMS_API_KEY_OPTION', 'sms_api_key');
define('SMS_SENDER_ID_OPTION', 'sms_sender_id');
// Include required files
require_once plugin_dir_path(__FILE__) . 'includes/class-sms-api.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-sms-admin.php';
// Initialize plugin
add_action('plugins_loaded', function() {
new SMS_API();
new SMS_Admin();
});
Core Functions
SMS API Class
PHP
<?php
class SMS_API {
private $api_key;
private $base_url;
public function __construct() {
$this->api_key = get_option(SMS_API_KEY_OPTION);
$this->base_url = SMS_API_BASE_URL;
}
public function send_sms($recipient, $message, $route = 'transactional') {
$url = $this->base_url . '/sms/send';
$data = [
'sender_id' => get_option(SMS_SENDER_ID_OPTION, 'SENDER'),
'recipient' => $recipient,
'message' => $message,
'route' => $route
];
$response = wp_remote_post($url, [
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json'
],
'body' => json_encode($data),
'timeout' => 30
]);
if (is_wp_error($response)) {
return ['success' => false, 'error' => $response->get_error_message()];
}
$body = wp_remote_retrieve_body($response);
return json_decode($body, true);
}
public function send_bulk_sms($recipients, $message, $route = 'promotional') {
$url = $this->base_url . '/sms/send-bulk';
$data = [
'sender_id' => get_option(SMS_SENDER_ID_OPTION, 'SENDER'),
'recipients' => $recipients,
'message' => $message,
'route' => $route
];
$response = wp_remote_post($url, [
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json'
],
'body' => json_encode($data),
'timeout' => 30
]);
if (is_wp_error($response)) {
return ['success' => false, 'error' => $response->get_error_message()];
}
$body = wp_remote_retrieve_body($response);
return json_decode($body, true);
}
}
WooCommerce Integration
Order Confirmation SMS
PHP
<?php
// Send SMS on order completion
add_action('woocommerce_order_status_completed', 'send_order_confirmation_sms', 10, 1);
function send_order_confirmation_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (empty($phone)) {
return;
}
$sms_api = new SMS_API();
$message = sprintf(
'Your order #%s has been completed. Thank you for your purchase!',
$order_id
);
$result = $sms_api->send_sms($phone, $message, 'transactional');
if ($result['success']) {
$order->add_order_note('SMS notification sent to customer');
}
}
WordPress Hooks Integration
User Registration SMS
PHP
<?php
// Send SMS on user registration
add_action('user_register', 'send_welcome_sms', 10, 1);
function send_welcome_sms($user_id) {
$user = get_userdata($user_id);
$phone = get_user_meta($user_id, 'phone', true);
if (empty($phone)) {
return;
}
$sms_api = new SMS_API();
$message = sprintf(
'Welcome to %s! Your account has been created successfully.',
get_bloginfo('name')
);
$sms_api->send_sms($phone, $message, 'transactional');
}
Complete Plugin Example
PHP
<?php
// Shortcode for sending SMS
add_shortcode('send_sms', 'sms_shortcode_handler');
function sms_shortcode_handler($atts) {
$atts = shortcode_atts([
'to' => '',
'message' => ''
], $atts);
if (empty($atts['to']) || empty($atts['message'])) {
return 'Error: Phone number and message are required';
}
$sms_api = new SMS_API();
$result = $sms_api->send_sms($atts['to'], $atts['message']);
if ($result['success']) {
return 'SMS sent successfully!';
}
return 'Error sending SMS: ' . ($result['error'] ?? 'Unknown error');
}
// Usage: [send_sms to="1234567890" message="Hello from WordPress!"]