How to turn any jQuery plugin into a WordPress one
WordPress and jQuery are both very famous for their plugins. In the case of jQuery, plugins allow developers to extend the library's capacities in order to create beautiful effects. In WordPress, they allow anyone to benefit from the work of others, as they come under the form of a module that you can easily activate from within the administration interface, and start using right away.
Creating one of those modules is not that difficult, but if you are not familiar with their syntax, it could end up being a headache. There are many jQuery plugins that have been transferred into a WordPress one already, but what can you do when it is not the case? How do you do when you have found the perfect jQuery plugin for your latest site, but it is not yet available as a WordPress plugin?
In this tutorial, we'll see how you can easily create a WordPress plugin from a jQuery one. We'll review what a jQuery, or a WordPress plugin is actually made of, so we'll find the elements they both have in common. From there, we'll show how it leads us to perform some simple operations to allow us to benefit from our jQuery plugins in a WordPress template.
I'm glad that you found my work useful. If you did, it might help some other people too. So why not helping me spread the word with a tweet? You could also buy me a coffee to thank me for my code and explanations if you prefer ;).
I hope you'll enjoy your download. Regards. Jeremy.
In this part, we'll analyse what a jQuery plugin is usually made of, where each of those elements should be placed, and how do we usually work with them. Throughout this article, we'll take for example the jQuery FancyBox plugin, (even if there is an existing WordPress adaptation for it already, it's just to have a concrete example).
Once you have included the plugin script within your application, most of the time you'll be asked to write a little snippet of code that will make your site or application use the plugin when required. Most of the time, those few lines of code you'll have to write are here to make the plugin work with a particular element of your page, and after a particular event, for example, you say that a FancyBox will appear after a click on a particular link, for example:
Once you have all your files ready, it's a common good practice to put the styles in the header, and the scripts in the footer, just before the closing
Part 2: How can we translate this into WordPress?
Now that we understand what the different elements we find in a jQuery plugin are, and where they are supposed to go, let's see how we can do that in WordPress.
How to declare a WordPress plugin?
A) In WordPress, plugins are stored in the /wp-content/plugins directory. So just go in there, and create a directory named after your plugin. In our case, we'll just create a directory called FancyBox.
B) In this directory, copy all your jQuery plugin files, (script, styles, images or anything that has to be included with the plugin).
C) Then, create a file called init.php. Init.php is the particular file we need to build to allow WordPress to recognize our plugin. Open init.php and copy the following information in it:
Plugin Name: FancyBox //Your plugin name (here we'd write Fancybox)
Plugin URI: http://www.jdmweb.com/resources/fancybox //Url of your choice
Description: Implementation of the FancyBox for jQuery //Brief PLugin Description
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
As you can see, in those few lines, we specify a name, a URL, a description and a few other similar piece of information. Those information are being used by WordPress inside the plugin administration interface as you can see on the following picture:
From this picture, we see that our 'plugin' has been recognized by WordPress, and contains all the information we wanted. It is time to make it work now. From what we've seen in part one, we need to tell WordPress to put some of our plugin files in the page header, and some in the page footer.
How to specify what goes in the Header ?
What we want to do is to put our plugin stylesheet inside the page header. In html, we do so by adding a link tag in the header. In WordPress, there is a specific method called wp_enqueue_style() (Codex ref here), that allows you to do that.
wp_enqueue_style() takes several parameters, mainly the name of your stylesheet and its source for it to work. You can also add dependencies (other styles that need to be included before), and a version number. So In our case, in order to tell our plugin to put its stylesheet inside the header, we would add this inside our init.php file:
Line 3 you can see the name we gave to our stylesheet, line 4 we specify its source, with the constant WP_PLUGIN_URL holding the path to the plugin directory. So if the user decides to move its plugin directory the path won't break. Line 5 false means that this stylesheet has no dependencies, and line 6 1.3.1 is the stylesheet version.
That's it for the header, in fact, to recapitulate, instead of adding the stylesheet in our header by ourselves as you would do in a normal application, you just ask WordPress to do it for you.
How to specify what goes in the Footer ?
In a very similar fashion, to add our scripts inside the WordPress footer, there's a function we can use as well. It is called wp_enqueue_script(), (Codex ref here).
wp_enqueue_script() accepts parameters that are very similar to wp_enqueue_style, meaning a script name, path, dependencies, en version. The last parameter is a boolean, and specifies whether you want to enqueue the script in your header (if set to 0), or in your footer (if set to 1), so in our case, because we want to enqueue them in the footer, we'll set the boolean to 1.
Let's then use wp_enqueue_script() to add our scripts:
Line 3 and 6, you can see wp_enqueue_script in action. We add our plugin scripts by specifying their name, path, dependencies, version and we place them in the footer with the 1 in the end. As you can see, the FancyBox script has one dependency, which is jQuery, and the FancyBox setup has 2, (jQuery and the FancyBox).
Time To Test
Your plugin is now ready to work. If you go back to the administration interface, in the plugin section, and activate the plugin, WordPress will add your files in the correct places, and your plugin will start working!
How to use the plugin on specific pages only?
Now that your plugin is activated, from the code we wrote in our init.php file, WordPress will automatically add our files in the header and the footer of every page of our application. This can be right if you effectively need to use this plugin on nearly every page of your site, but if it is not the case, it's more of a waste of resource, and a bigger page load time for nothing.
I'm going to show you a little technique if you'd like to include the plugin only on certain pages. It's very simple
The trick is to gather the little snippets of code we just wrote, and to put them into one function, that we're going to call FancyBox_wp_setup():
This has actually 2 different effects. It prevents WordPress from including your pages on every page, which is something we wanted, and it also gives you access to the function FancyBox_wp_setup(), that you can call on every page template you want to use the plugin on.
When you find the perfect jQuery plugin for your design, integrating it properly for a WordPress use is just a few lines of code away! I hope that next time you'll find a plugin you wish to use, you'll remember this article and you'll turn your plugin into a WordPress one in 5 minutes.
Other Useful Resources
Ever looked for a straightforward way to integrate you instagram pictures on your website? Without a plugin nor an iframe? Well this article featured on the NOE interactive's blog presents how you can do so with PHP.
It's a neat and simple solution that uses the instagram's API to get a particular users' latest pictures, and then display them in a ul list.
It is accessible to most developers as it is a "personalize, copy and paste" kind of solution. Just two steps are required, configuration, and implementation.
In this article featured on NOE interactive, you'll learn how you can install the solution on your website. There's a complete step by step guide as well as a demonstration. If you wish to download the solution, it's also hosted on Github.
Building dynamic forms is a task that a lot of web developers will have to do at some point. By dynamic I mean forms that change based on what the user inputs.
A basic example of dynamic forms, are those that allow the user to add the same group of information several times. For example, an attendance form, where you can add several persons, a booking form where you can add several tickets, a membership with several users, a media page with several pictures or videos and so on, the possibilities are countless.
In order to facilitate the development of such forms, I created a little plugin, that allows you to dynamically clone a specific set of information that will be repeated. So if you ever searched a way of cloning forms, fieldsets or groups of input, this could really be helpful.
As a lead developer, I watch the web development market for solid trends (not the hype ones), and see if they could be a tangible addition to our workflow. Like Angular a few years ago, React has gained enough momentum to catch my attention. I decided to try it out in a side project to get a better understanding of how it works. Here are a few things I've noted from our first encounter. This is a beginner's point of view, not a React expert's one, so if you see any mistakes, don't hesitate to let me know, I'll update the article accordingly.