It was 4 pm yesterday and I was checking out Facebook when I noticed a post by a WordPress friend asking, “Anyone know how to implement a feature to send a link via a text message to user's phone?” This reminded me of off-line work I'd done a couple years ago with a company called TextMarks. There had to be a way. So I mentioned that I would look at it later in the evening – and what a fun night it turned out to be.
Everything starts with a Text Gateway
Before you can code anything related to text messaging, you need a text gateway. This is the server that will process requests and allow you to automate the sending of text messages to actual phones. Now there are a lot of vendors out there – and just a look at their web sites makes you run. But a few are reputable. Since I already had working knowledge of TextMarks I looked them up again. If not, I would always start any digital telephony journey by looking at Twilio – my all-time favorite. So I pulled up TextMarks' site and found their API docs.
I want to work with Crowds
Now I know there are ways to work with different text gateways where it's just a direct message processor. I send a specific phone number a specific message. But I don't think that's the right model – at least not for the kinds of things most people want to use texting for. For the most part, what I hear about is dealing with things in bulk. Letting a whole group of people receive the same text all at the same time (kind of thing). And that means thinking about the project differently (and why TextMarks works well).
TextMarks lets you register a “term” that others use to subscribe to your group. So people can text “WORDPRESS” to 41411 (without any web forms) and they'll join my text group. This group can then later be messaged at any time (all in bulk). That's what I call cool! Different plans determine how many terms you can register. At one point they even let you buy t-shirts with your code on it [NOTE: Not sure if this is still available.]
Did I mention I love GravityForms?
While TextMarks makes it easy to join a group via your phone, what I wanted was a form that would let a user put in their phone number. No issue – when I think of forms, I think Gravity Forms. Creating a form with one field (for a person's phone number) is really easy.
Gravity Forms offers Hooks
While the form was easy, the API from TextMarks requires a POST, and when you click on the Advanced tab in the form creation, one option is a “redirect” but it's more like a GET (and it's not even that, it's just a redirect) than a POST. Thankfully, that's not where things end with Gravity Forms. They have countless hooks available for you to tie into. What's a hook? Basically it's a spot where they let you connect into their own logic and add your own.
In this case their hook is called “gform_after_submission” which means you can tie into their logic right after the form has been submitted. They also have a targeted hook that lets you specify exactly which form (and you do it by appending the form id). So since this form was #4, the call I wanted to use was “gform_after_submission_4”. That's truly nice!
The other thing I would need is the value of the form submission. And it doesn't get easier than:
$phone = $entry
Let's see some Action
Actions are where you write your own logic, and my logic didn't have to be too much. In fact, I would need to call a WordPress function which meant I would barely have to write any code at all. Since I wanted to call a remote server, WordPress offers me “wp_remote_post()“. If you're thinking – “wait, I don't know how to do this stuff” don't you stress. The codex (which is a fancy word for the code encyclopedia) tells you exactly what to do.
So the hardest part was filling in the blanks based on the API call that TextMarks needed.
wp_remote_post('http://help.api2.textmarks.com/Anybody/invite_to_group/', array( 'method'=> 'POST', 'body' => array( 'api_key' => 'put your key here', 'tm' => 'put your term here', 'user' => $phone));
7 lines of code that were so sweet
Now, if you like readable and commented code, maybe it's 16 lines of code.
How it Works
When someone submits the form the following happens:
- Form is submitted.
- Form data is captured and made available via the gform_after_submission hook
- API is called and passed info via POST
- TextMarks sends out a text confirming you want to join the group (you can configure welcome text)
- TextMarks (optionally) sends a second one-time text now that you're in the group (could be a link, etc).
So, how could you use this? Let me know!