Writers want to stay in touch with their readers. Sending bulk SMS is a great tool to have in your toolbelt. Let’s build a WordPress plugin in PHP so you can send text messages to readers about topics they care about.

WordPress Plugins

Plugins let you extend and add functionality to your WordPress site. Since we are adding an entirely new function to WordPress, we have to use a plugin to achieve this.

First, we need to install WordPress if you haven’t already. You can download and install one locally by following the installation instructions here. I have my WordPress installation path as localhost/sendex/ . I’ll be using it as my reference all throughout this tutorial.

Once you are done with the installation, log into your WordPress admin dashboard. This is where we will be working from now on. The admin dashboard should be localhost/sendex/wp-admin assuming you used the same installation path that I used.

Assuming your installation path is http://example.com/ , you can get the admin dashboard by opening http://example.com/wp-admin in your browser. Log in with your username and password. If the login was successful, you’re ready to go.

Getting a Twilio SMS Account

Head to Twilio to create an account if you don’t have one already. After that, navigate to the Twilio Console and note down your account sid and auth token keys. They should look like:

ACCOUNT SID = AC90d5e*******************

AUTH TOKEN = ***************************

Setting Up Our WordPress Plugin

Now we’re ready to write some code. Here’s a rundown of what we’ll do with WordPress:

Get the plugin boilerplate

Create a settings page

Implement the sending messages functionality

The best way to begin with a new plugin is by working on the incredibly useful WordPress Plugin Boilerplate. You might be asking why are we using a boilerplate instead of building from scratch. This boilerplate will get you started quickly with a standardized, organized and object-oriented foundation – basically, everything you want if you started from scratch.

To get started, just go to the WordPress Plugin Boilerplate Generator and fill-out the form then click on the Build button. For example, here are the fields I filled in:

Plugin Name = SEND SMS

Plugin Slug = sendex

Plugin Uri = http://menusms.com/sendex

Author Name = Onwuka Gideon

Author Email = gideon.onwuka1@gmail.com

Author Url = http://menusms.com/dongido

You can fill in whatever you want in the fields above. I’d suggest you use the same details I entered. This will make it easier to follow along with the code. You can change the details later once you fully understand the code.

After downloading the boilerplate above, extract it into your WordPress plugin folder i.e. localhost/sendex/wp-content/plugins .

Once you’re done, go to the admin plugin page localhost/sendex/wp-admin . If you are logged in, you should see our new plugin in the list of plugins. Activate the plugin by clicking on the activate link.

Now that our plugin is installed, let’s review our boilerplate folder structure.

When you open the plugin folder sendex , you will notice that the plugin has 4 folders: admin , includes , languages , public and some other files.

admin/

This folder contains all our admin facing code; including css, js, partials folders and a php file class-sendex-admin.php.



includes/

In this folder, you will find:

The main plugin PHP class class-sendex.php where we will add all our actions and filters

where we will add all our actions and filters The activator file class-sendex-activator.php

The deactivator file class-sendex-desactivator.php

The internationalization file class-sendex-i18n.php

The loader file class-sendex-loader.php which will basically call all our actions in the main class file

languages/

This folder which is a ready to use .pot file to make your plugin in multiple languages.



public/

The public folder is the similar files as our admin folder except that all codes here are for public facing functionalities.



Now we are left with four files:

LICENCE.txt : GPL-2 license

: GPL-2 license README.txt : This will include your plugin name, compatibility version, and description as seen on the plugin page in the WordPress repository. This is the first file we will edit.

: This will include your plugin name, compatibility version, and description as seen on the plugin page in the WordPress repository. This is the first file we will edit. uninstall.php : This script is called when the user clicks on the Delete link in the WordPress plugin backend.

: This script is called when the user clicks on the Delete link in the WordPress plugin backend. sendex.php : This is the main plugin bootstrap file. You will likely edit this file with the version number and the short description of your plugin.

Create a settings page

We’ll need to add the settings page to our admin panel so we can manage our API credentials easily.



