wa.me Links Explained: Formats, Encoding and Gotchas for Pre-Filled WhatsApp Messages
Click-to-chat links ( https://wa.me/ ?text=) look trivial, but they break in surprisingly common ways. Here’s what to get right. The number format Full international format: country code + number No +, spaces, dashes or brackets Drop the national leading zero: UK 07700 900123 becomes 447700900123 A quick JavaScript helper: js function waLink(number, message = '') { const digits =…
Click-to-chat links (https://wa.me/?text=) may seem simple, but they can break in unexpected ways. To ensure proper functionality, there are a few key points to keep in mind.
First, use full international format for the phone number, which includes the country code followed by the number without any +, spaces, dashes, or brackets. For example, UK number 07700 900123 should be entered as 447700900123.
A helpful JavaScript function for creating these links is:
```js
function waLink(number, message = '') {
const digits = String(number).replace(/\D/g, '');
const text = message ? '?text=' + encodeURIComponent(message) : '';
return 'https://wa.me/' + digits + text;
}
```
For instance, waLink('+91 98765 43210', 'Hi, I need help with order #1042') generates https://wa.me/919876543210?text=Hi%2C%20I%20need%20help%20with%20order%20%231042.
When encoding the message, it's important to encode the entire message, not just spaces. Characters that can break links when left unencoded include space (%20), new line (%0A), ampersand (%26), hash (%23), question mark (%3F), and plus (+). JS users should use encodeURIComponent(), while PHP developers should employ rawurlencode(), which converts spaces to %20.
A few things to note:
1. On desktop, the link will open WhatsApp Web, not the app itself. The pre-filled text can be edited by the user and will not be sent as is.
2. To send automated messages like order updates or reminders, you'll need to use the WhatsApp Business API.
3. The phone number must be registered on WhatsApp. If not, the user will see an error.
For WordPress site owners looking to add WhatsApp capabilities, there are several methods available, from simple links to full API automation.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to