How do you starting writing a WordPress plugin?
Today, I started writing a post about the beginning steps of making a WooCommerce Extension, when suddenly it occurred to me that I should start from the beginning instead.
What are some skills you needed in writing a WordPress Plugins? What are some things you need to think about when starting? There is a lot to cover just in these basic questions.
Making a simple plugin is actually easy. Truthfully, there are resources available to you on WordPress.org which detail everything you need to actually commit the act of writing a plugin. The gist of the article is that there are a few simple steps:
- Choose a unique name. Usually this means having a unique identifier that shows your plugin was written by you, and a generic name appended to that which describes the function of the plugin. For example, I might call my plugin ‘loushou-hello-world’, “loushou” being my screen name on WordPress.org, and “hello-world” being a description of what the plugin does.
- Create a directory in your /wp-content/plugins/ folder using that name. In my case I would use /wp-content/plugins/loushou-hello-world/.
- Create a file in that directory using the name of your plugin. In my case: /wp-content/plugins/loushou-hello-world/loushou-hello-world.php
- Add some header comments to that file that tell WordPress a little about your plugin:
<?php /** * Plugin Name: Loushou - Hello World * Plugin URI: http://quadshot.com/hello-world-plugin/ * Description: Adds a 'hello world' message to the admin. * Version: 0.1.0 * Author: Loushou * Author URI: https://profiles.wordpress.org/loushou/ * License: GPL2 */
- Add some functionality to your plugin so that it does what you say it does…
You need to have a strong ability to figure out and solve problems. You also need to be able to ‘forward-think’ because you don’t want to have to keep rewriting your plugin from scratch when you want to add something awesome to it. So basically, you need to be a programmer with a skillset centered around PHP development.
In addition, if you want to make the plugin available on WordPress.org, then you will also need at least a basic understanding of SVN and how to use it (or at least the ability to Google it).
The requirements seem pretty straight forward to this point, huh? Great. Well, now that we are past that, there are several things to think about when making a plugin. Everyone’s list is different, but this is my list of things you should know or seek out before you write a plugin:
- What version of PHP and MySQL should I be compatible with, when I make a WordPress plugin? You can find this answer in the Requirements page on WordPress.org, but the short answer is PHP 5.2.4 (PHP 5.4 or higher is recommended) and MySQL 5.0 or higher.
- What types of environments should I make my plugin compatible with? All kinds. You have no idea where your plugin will be installed, what software they are using on that system, and the OS could even be something you’ve never heard of. An experience PHP programmer knows how to make their code cross platform compatible, which is not necessarily as easy as it sounds.
- What programming techniques should I use when making the plugin? Remember, WordPress uses PHP 5. That means that you have an Object Oriented language to work with. I cannot tell you how many times I see plugins that are written like they have been around since PHP 4. There is no reason to make a completely procedural plugin. Use your PHP 5 knowledge and make your plugin a class or object (or series thereof), not a list of functions that have your pseudonym in the function name. Using OOP can give you access to all kinds of time and resource saving techniques, and it can help organize your code. So if you want to make a good plugin, stop using PHP 4 style programming.
- What are Actions and Filters in WordPress, and how do I use them? You can find out the basic information on the Plugins API page, but the gist is that they are key to building in your own functionality or modifying the existing functionality. It is also very important that you understand how to incorporate these into your plugin, because you never know how it may be used or what functionality someone may want to add to it.
- How do I find out how something works in core WordPress? I would say this is by far the most important thing on the list. You have to not only have a need to go looking for the information, but you need to be bold enough to actually go looking, to find what you need, and to fiddle with it to figure out how to use it. DO NOT be afraid to hunt something down in core, because it will always give you awesome information you never knew before, that you can use now and in the future.
- Where can I ask questions? If you cannot figure something out, do not be afraid to ask. The community is very knowledgeable. If you have a problem, or need an answer to something, either someone out there has already asked the question or someone out there knows the answer. That is not to say ‘start by asking’. Explore on your own first, checkout the Documentation since there is a probably a page that explains what your are looking for, and if you hit a road block, ask. Everyone needs a hand sometimes, and asking the community will almost always get you some help.
That’s it for now. Hopefully you have an understanding of what types of information you need to gather before starting to write a plugin, and maybe you have a few new resources to pull from. Next time, we will go through actually creating a basic plugin. That process should get you a solid footing on making your own. Past that, we will talk about OpenTickets Community Edition, and some of the obstacles we had to overcome when making that plugin.