Adding WordPress Code to Plugins

Last night I attended one of the excellent Arizona WordPress meetup group events.  A presentation by Brandy Lawson demonstrated how to create a WordPress plugin as a means to include code snippets found on the internet into your website.

There are several benefits to creating a plugin versus adding the code into your theme’s functions.php file.

First, plugins are theme independent.  Your code will run regardless of which theme you are using.  If down the road you decide to refresh the look of your website with a new theme, your custom modifications won’t have to be ported to the new theme.

Another reason is that plugins can compartmentalize your modifications.  It is easy to look at the plugins page in your WordPress dashboard to activate and deactivate a feature.  If you paste 1000 lines of PHP code into your functions.php file from a dozen different sources, management and comprehension of your functions.php is harder.

Some website designers will warn about having too many plugins installed on your website.  The fear is that including too many plugins will slow down your website.  This is only true if the code within the plugin is slow.  Slow code will slow your website regardless of where it is placed.  Fast code spread over 100 plugins will not.

This morning I saw a woocommerce tip on twitter.  It supplied a piece of code to use the customer’s email address as their username. I can’t tell you how many times customers forget their username.  Being able to sign-in with an email/password instead of a username/password is a much better user experience.

For your convenience here is the code suggested by the tweet:

While you could paste it into your functions.php, this is a perfect example of something that can exist independent of your theme.

To convert this PHP code into a WordPress plugin, all you have to do is add a special comment header.

Here is what the plugin version of the code looks like:

Save this into a descriptive filename (e.g. woocommerce-add-email-for-login.php) and upload it to your wp-content/plugins folder. Now you can manage this email for login feature through your WordPress plugins page!

As you can see, it’s easy to create a WordPress plugin.  The extra few minutes it takes to build a plugin pays off over the lifetime of your website.