I’m going to discuss a functionality plugin again. I hope you’ll read this article all the way through. I fully intend to help you make your WordPress life so much easier. We’re going to make a site-specific plugin, and I’ll even give you the bare bones for free! I avoided making my plugin for years, and realize how painful that made everything. These plugins are amazingly easy, and essential really.
So first up, let’s show how to make one, and then we will get into why. First up on your computer just make a folder, call it what you will. I tend to name it based on my site, so vpplugin, rvoodooplugin, etc. For simplicity and maximum compatibility, just keep the folder name lowercase and all one word.
Now inside that folder, make a new text document (Do not use a word processor for this, like MIcrosoft Word) using a plain text editor like wordpad or notepad, or a code editor like notepad++. I highly recommend notepad++ since it’s free, has syntax highlighting, etc. This is the content for your new file:
<?php /* Plugin Name: Site Specific Plugin Description: My Site Specific Code */
Or, you can get fancy with your plugin info, here’s mine:
<?php /* Plugin Name: VoodooPress Functions Plugin URI: https://voodoopress.com Description: Non-theme specific functions for VoodooPress. Author: Rev.Voodoo Author URI: http://www.rvoodoo.com Version: 1 License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */
Honest, that’s all you need. Now save it as a php file. I believe you can call it whatever you like, I always stick with plugin.php. If you use notepad++ you can just name it and save it. When using notepad or wordpad, you can name it as plugin.php, just be sure to change the dropdown on file type to ‘all files’ which allows it to actually be saved as a php file. If you don’t do that, it is assumed to be a text file, and it gets called plugin.php.txt.
So, now you have your php file inside the folder. Zip it into an arvive. In windows, that’s as simple as right clicking on your new folder and selecting send to -> Compressed (Zipped) folder.
That’s it, yer done! You can now install the plugin by uploading it through the add new plugin interface, and you can activate it! If you don’t want to go through these motions, here is a bare plugin ready for you to install and work with!
So now that we have our plugin all set to go, let’s discuss the why and how shall we?
What goes into this plugin? Well, surely you have cruised around the web looking for bits of code that you can use to do a variety of things. Someone has provided that code to you with the instructions to ‘drop it into functions.php in your theme.’ Well, this plugin works just like functions.php, only better.
Not necessarily every bit of code should move from functions.php to this plugin though. Here’s how I decide. I ask myself “Is this a site function, or a theme function?” What’s the difference? Well, a theme function is code that modifies portions of the theme. Maybe I am modifying something directly out of the 2011 theme, twentyeleven_posted_on for example. That goes into functions.php. A site function affects the whole site. An easy way to look at it is, If you switch themes, should this piece of functionality still exist? If so, it goes in the plugin.
Here’s some examples from my site:
Theme Specific (in functions.php)
- registering new sidebars to position in 2011 theme
- customizing 2011 header size
- adding and removing 2011 default header images
- removing the singular body class from 2011 so I can add sidebars to the single view
- adding a new colour scheme to 2011
Site Specific (in my plugin)
- deregistering stylesheets and scripts from loading for various plugins
- building custom widgets, including registering scripts and styles for them
- adding in my favicon, and loading jQuery from Google
- registering a meta box framework and defining meta boxes my site relies on
- changing the new user registration email from name and email
- login page changes (new stylesheet, link change, etc)
- deregistering the link manager
I do plenty more, but I think that’s enough examples to highlight my way of thinking.
Now, the benefit. Why bother? Well for one, it keeps your theme code neat. You don’t have tonnes of code in your functions.php. But a more important thing to consider is portability. If you keep your theme specific stuff with the theme, and site specific stuff with the plugin you can easily switch themes. Load up another theme, and everything theme specifc stays with the theme, but everything you rely on for your site stays active. No more worrying about copying and pasting between themes!
Now here’s my absolute favourite part of this, safety! Have you ever pasted some code into functions.php and POOF, you get a fatal error, or the white screen of death? It’s scary and inconvenient. You have to log on using ftp, and correct the problem, if you can figure out what you did wrong. Heck, you might even have to learn FTP first.
Well when something goes wrong in a theme, it takes down your whole site! Not so with a plugin. If you are working on a plugin which is live on your site, and you trigger a fatal error, the plugin simply deactivates. All you get is an error message. You can go right back into the plugin editor (Plugins->editor) and fix it. No FTP, no dead site.
I’m as guilty as many other people for recommending to drop code into your functions.php. But after reading Otto’s post, I’m going to start recommending people make a plugin, and put code there. If you help people out, I’d recommend you do the same.
Hopefully, I’ve given you some points to consider, and I hope you are working on your own plugin right now as we speak!