Can't find a WordPress plugin that does exactly what you are looking for? Well, why don't you create your own?
Plugins for WordPress are simply PHP scripts that change something on your cheap hosting website: from a small change in its design to more complex actions such as naming actions where emails should automatically be sent out. Plugins are not to be confused with themes. Changing the theme of your cheap hosting site alters the way it looks to visitors, while plugins change the way it works altogether.
Plugins allow you to dictate types of custom posts, provide Google Translate functionality to users of your site, add social networking feeds, practice more effective SEO, and even add e-commerce functionality.
Create Your Own WordPress Plugin: Theme vs. Plugin
Every theme has a functions.php file associated with it (and if you are thinking about creating a plugin, you have probably discovered this while messing around with the theme code.) This functions.php file gives you the ability to add plugin-style functionality to the theme. I know you are probably asking one question right now: if I can build plugins into my theme, what's the point of plugins anyway?
There are times you might want to create your own, but most of the time, a plugin that already exists will do just fine. For example, you want to change the length of your posts' excerpt default length. This can easily be done without causing any harm directly in the functions.php file. However, if you want to add a social aspect where users can interact with each other to your site, a plugin is best.
The plugin works the same way, no matter what theme you have. However, if you alter the functions.php for some reason and switch themes, you'll find those changes you made won't work anymore. You have to do it all over again in the new theme.
Create Your Own WordPress Plugin: How To Create A Plugin
Let's get started creating your very first plugin! You need to create a folder containing a file that is a mere one line of content. Head to the folder ‘wp-content/plugins' and create a new folder. We'll call it ‘thebestpluginever' for our example. Within that folder, make a file called ‘thebestpluginever.php'. Open that file in a text editor and paste the following:
Of course, you technically only need to add the name of the plugin. However, if you plan on distributing your plugin to the world, it is nice to add as much information as you can.
Now, head to the back end and activate your plugin. Now you're done! The example plugin we just created doesn't do anything at all, but is still a functional, active plugin.
Create Your Own WordPress Plugin: The Structure Of Plugins
When you want to create a complex plugin, it's best to split it into multiple files and folders to keep it easier for you. Of course, you don't have to do this if you don't want to, but it's a good idea nonetheless!
You should strive to find a balance between usability, structure, and minimalism. Dividing your plugins into many files if you have to, but don't go nuts. If you need a reference, look at the way plugins such as Akismet and WP-PageNavi are structured.
What's In A Name?
Be careful when choosing a name for your plugins, classes, and functions. Let's say your plugin's function is to give your users one-click sharing of your content with others. You could call it ‘share' and the main function ‘share_it', but these are quite generic. You run the risk of going up against a plugin with a similar name. Therefore, give it a unique name. Use a pet name, your own name, and so on: ‘timbuktu_share' would be a great way to assure it doesn't conflict with a similar plugin.
Keep It Safe
Looking to distribute your plugin? Then you should be thinking about security! Other people's websites are at risk, not just yours.
Safety comes down to two things: 1. the plugin cannot distribute malicious data; and 2. assures that the user has administrative rights to perform whatever the plugin does and that they initiated the request to do it. To protect against the first problem, one must filter user input, combat SQL injection attacks, etc.
Clean It Up!
There are a variety of plugins that end up leaving useless data behind, effectively slowing things down. You need to be sure your plugin can remove this data, like meta data, tables, etc.
Three hooks that WordPress provides address this problem:
- register_activation_hook() – This function runs as soon as your plugin is activated. It heads to the main plugin file first, and then to the function you are attempting to run. Use it to see the version of the plugin, upgrade between versions, see the proper PHP version, etc.
- register_deactivation_hook() – Works in the opposite way as the plugin described above, running when the plugin is deactivated.
- register_uninstall_hook() – This is for the moment the admin deletes the plugin in the back end. It removes all of that useless data like tables, settings, etc. The only problem: the plugin must run for this to work. If your plugin cannot uninstall using this method, you can make an uninstall.php file.
The goal in all of this : make a plugin that can be installed and run for a length of time (let's say 15 years), and then deleted without leaving a bit of data behind, whether in the database, file structure, or website in general.
What if you are developing this plugin for a large community? It's a good idea to provide documentation for your code. People appreciate this, and you will too: will you remember what each and every function does down the road?
As for coding standards, following them is wise if you want your code to be compliant with the guidelines set forth by WordPress.
These are the basics of creating your very first WordPress plugin. Is there something else you would like to see regarding WordPress? Let us know so that we may discuss it in a future article!
Photo courtesy of Brad.K via Flickr Creative Commons