“Chat with us now!” We’ve all seen those popups on popular websites, letting you know that if you have a question or need some help there is someone (or something) that you can turn to. Sometimes though chat is just not the right medium for getting your question answered or the support you need. For a higher fidelity experience, stepping it up to video is the best option. In this post I’ll show you how using Twilio Video and a little bit of JavaScript and PHP, you can build a Video Chat plugin for WordPress, the world’s most popular CMS system. Never built a WordPress plugin? Don’t worry, it’s a straight-forward and simple process. Want to head straight for the finished product? No problem. Grab the complete code for the plugin from . Otherwise, let’s get building! Getting Setup Before we get coding we’ll need to gather a few items that we’ll use to build our plugin. First, you’ll need an installation of WordPress that allows plugins. This can be either , or remotely on a hostingprovider like . Unfortunately WordPress.com does not allow you to upload custom plugins. Next, you’ll need a Twilio account. If you don’t already have one ! With a WordPress install and Twilio account in hand we can begin building our plugin. Start with Structure WordPress plugins are bits of PHP code, JavaScript and CSS that can extend and expand the functionality of your WordPress site. When you install a plugin, WordPress places the assets of that plugin in a new directory under the /wp-content/plugins/ folder of your WordPress installation. That’s where we will start building our new plugin, by creating the folder and file structure that we need. Create a new folder named twilio-video under the plugins directory. In that directory, create a folder and file structure that matches the list below. For now you can leave the files empty, we’ll add content to them as we go along. /wp-content/plugins/twilio-video twilio-video.php randos.php twilio-video.html /js twilio-video.js /csstwilio-video.css /lib With the basic plugin structure in place let’s start adding some content. Grab the CSS styles for the plugin from the and place them in the twilio-video.css file. Now is also a good time to add the twilio-php helper library to our project. Because Twilio Video is still in beta, you’ll need to grab the help library from either the , or from the . Once you have the helper library files copy them into /lib folder. Finally, we’ll need one final prepared resource. A random name picker which we will use later on to assign an identity to each customer who connects to an agent. Open randos.php and drop the code from the into it. Awesome. We’re now ready to write some code for our plugin. To begin we need to create a plugin header which is simply metadata that WordPress uses to know some stuff about our plugin. That metadata goes into the twilio-video.php. Open that file in your favorite editor and add the following: PHP <?php /** * Plugin Name: TwilioVideo */ ?> 1 2 3 4 5 <?php /** * Plugin Name: Twilio Video */ ?> The Plugin Name header is the only required header, but there are that you can use to provide even more details about your plugin to WordPress. At this point the PHP file is now a valid WordPress plugin, and you can head into your WordPress admin site and . The plugin doesn’t do anything yet so it’s pretty boring. Lets spice it up by having WordPress add the Twilio Video JavaScript SDK and our plugin UI to any page it renders. Back in twilio-video.php, call the function to wire up the action. This action lets us tell WordPress about a function defined in our plugin that we want it to call when it’s figuring out which resources to include in the rendered web page. In that function we’ll call the wp_enqueue_style and wp_enqueue_script functions to let WordPress know about the scripts and styles our plugin needs to include in the web page. PHP <?php /** * Plugin Name: TwilioVideo */ define('TWILIO_VIDEO_PLUGIN_URL', plugin_dir_url(__FILE__)); add_action( 'wp_enqueue_scripts', 'twilio_video_enqueue_scripts' ); function twilio_video_enqueue_script() { wp_enqueue_style('twilio-video-css', TWILIO_VIDEO_PLUGIN_URL . 'css/twilio-video.css'); wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'twilio-common', ' wp_enqueue_script( 'twilio-conversations', ' wp_enqueue_script( 'twilio-video-js', TWILIO_VIDEO_PLUGIN_URL . 'js/twilio-video.js' ); } ?> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php /** * Plugin Name: Twilio Video */ define ( 'TWILIO_VIDEO_PLUGIN_URL' , plugin_dir_url ( __FILE__ ) ) ; add_action ( 'wp_enqueue_scripts' , 'twilio_video_enqueue_scripts' ) ; function twilio_video_enqueue_script ( ) { wp_enqueue_style ( 'twilio-video-css' , TWILIO_VIDEO_PLUGIN_URL . 'css/twilio-video.css' ) ; wp_enqueue_script ( 'jquery' ) ; wp_enqueue_script ( 'twilio-common' , ' ) ; wp_enqueue_script ( 'twilio-conversations' , ' ) ; wp_enqueue_script ( 'twilio-video-js' , TWILIO_VIDEO_PLUGIN_URL . 'js/twilio-video.js' ) ; } ?> For our plugin we’re enqueuing that single twilio-video.css file, jQuery, the and the twilio-video.js file we created earlier. Once the scripts and CSS are in the rendered HTML we can use the API’s and styles they contain to create our plugin UI. Open the twilio-video.html in your text editor and add the following HTML: XHTML <div id="twilio-video-container"> <div id="agent-prompt">
Speak with an Agent
<button id="enter-queue">Connect</button>