Developer documentation for the Twilio SMS integration in the WPResidence real estate CRM. Entry point is libs/twilio.php.
Send API
wpestate_crm_send_notification_sms( $event, $data )
Flow:
- Check global flag crm_twilio_enabled. Abort if falsy.
- Check per-event flag crm_notify_sms_{event}_enabled. Defaults to true unless explicitly ‘0’.
- Validate all three credentials present (crm_twilio_account_sid, crm_twilio_auth_token, crm_twilio_from_number).
- Determine recipient phone: $data[‘agent_phone’], or user meta crm_phone, or user meta billing_phone.
- Resolve placeholders in the body template via wpestate_crm_resolve_placeholders().
- POST to Twilio REST API.
- Record sms_sent activity on the contact on success.
Twilio REST Call
- Endpoint: https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json
- Method: POST via wp_remote_post()
- Auth: Basic Auth header, value base64_encode(“account_sid:auth_token”).
- Body: form-encoded with From, To, Body.
Settings
| Key | Default |
|---|---|
| crm_twilio_enabled | false |
| crm_twilio_account_sid | empty |
| crm_twilio_auth_token | empty |
| crm_twilio_from_number | empty |
| crm_notify_sms_new_contact_enabled | true |
| crm_notify_sms_new_contact_body | see below |
Default body template: New lead: {contact_name} ({contact_email}). {listing_title}. View: {crm_contact_url}
Event Integration
Currently called from wpestate_crm_on_new_contact_notification() (libs/notifications.php) after email sending if the function exists. The design anticipates future events (deal_won, task_overdue) – add them by calling wpestate_crm_send_notification_sms($new_event, $data) from the relevant hook.
Test SMS AJAX
| Property | Value |
|---|---|
| Action | wpestate_crm_send_test_sms (no _nopriv – admin-only) |
| Nonce | via wpestate_crm_verify_ajax_nonce(‘send_test_sms’) |
| Capability | manage_options |
| POST param | phone |
| Test message | Test SMS from {site_name} CRM. If you received this, Twilio is configured correctly. |
Returns JSON success/error with the Twilio response body or a descriptive error (missing credentials, invalid phone, HTTP error code from Twilio).
Placeholder Support
Uses the shared wpestate_crm_resolve_placeholders() function – same 20 placeholders as the email module. No SMS-specific placeholders.
Error Handling
- wp_remote_post() errors: logged, function returns false, no activity record.
- HTTP >= 400: logged with response body, no activity record.
- Missing phone on recipient: silent skip (expected – not every agent has a phone on file).
Security
Credentials stored plain-text in the CRM settings array (standard WordPress pattern). For production deployments, consider storing the crm_twilio_auth_token in wp-config.php as a constant and reading it via a filter rather than exposing it in the admin UI.
Extending
- Add a new SMS event by registering a new settings pair (crm_notify_sms_{event}_enabled and crm_notify_sms_{event}_body) and calling the send API from the relevant hook.
- Replace Twilio with another provider by forking libs/twilio.php into a child plugin and overriding wpestate_crm_send_notification_sms with your own implementation.