WordPress Custom – Make Your Own Custom Functionality Plugin

written by William Patton on August 6, 2012 in WordPress Custom with 2 comments

Functionality Plugin

WordPress custom - WordPress tips, tricks, customization and more.

Improve upon what you create

A functionality plugin is a custom made plugin for your site. It holds any kind of changes you make to your site, any added features and functions and generally makes your site’s code easier to manage. If you need to add a new custom post type, change how things load or add a shortcode this is the place to do it.

You can accomplish any number of things with a custom functionality plugin for your site. From adding custom post types and building custom queries to changing the login page logo.

Why it makes sense to use a Functionality Plugin

In the world on WordPress it’s not uncommon for users to want to add something to their theme, not every theme works the way everyone wants it to – it may look how you want but not function the right way for your site. If you want to add a custom function the accepted practice for doing this is adding the custom code snippets into your theme’s functions.php file. Personally I suggest making a child theme based off the theme you want to customize, that eliminates the upgrade persistance problems I will tell you about below. When we do it like this we bloat the functions.php file – making it longer, harder to read and difficult to search.

Often when we are adding custom code to a theme that is for our own benefit we don’t add comments – sometimes we don’t even add comments if it’s code we intend to share! Sure you can read through the code, follow the flow and work out what it does, but that’s a lot of work just to find something and usually we only need to check it or modify it ever so slightly.

It makes sense to organize your customization code and it makes even more sense not to loose your customization during a theme upgrade – take your additions out of the theme and place them in your very own custom plugin.

Why you need a custom Functionality Plugin for you site.

Let’s face it: not all of us know how to make our own theme but we all know how copy/paste works, don’t we? If we want something on our site and can’t code it ourselves we simply Google what we need and paste the code snippet into our functions.php file. That way works perfectly and you may think there is no need to do it another way but what if you want to change your theme? You have to go through your functions.php file, find your custom code – which (especially if you haven’t commented your code) can be troublesome at times to segregate the theme’s original code from your own – and add it to your new theme. It sounds simple enough but for some people it can prove tricky.

Another major flaw in adding your own snippets to the functions.php file is that when a theme upgrade is performed EVERYTHING is overwritten. Anything you have added to the theme is gone without a way of restoring it unless you have a back-up of the file that you created beforehand, or you are using a child theme. It seems like a flaw and there was a workaround created to fix it: the myhacks.php file but since WordPress 2.8 this has been disabled in favor of the child theme method (and for security reasons) and people simply reverted to adding stuff to functions.php once again.

Using your own custom site functionality plugin eliminates all of the pitfalls I have detailed so far, and probably many more that I haven’t thought of yet.

Benefits of using a custom plugin

  • Your changes aren’t overwritten during a theme update.
  • Your code is more manageable.
  • Easier to find what you need.
  • It’s modular – you can move it between sites if you want.
  • When you change your site’s theme you won’t have to copy all your code to the new theme – and it’s when not if. If you have a site for a year your likely to change your theme at some point, what if you have your site for 5 years?

When to use a Functionality Plugin and when to use functions.php

With how I’m ranting on about the benefits of using a site specific plugin it would be more than understandable if you thought “I’ll use my plugin for everything” but that would be wrong. It’s very debatable what you should and should not use the plugin for. A simple way to decide what should go into the Functionality Plugin is – will I be using this 2 years from now, even if I change my theme? Anything that is directly connected to your theme, like a layout change, should go into the functions.php file.

Things like setting your default thumbnail sizes should go in your functions.php, setting up a custom post type should be done in your plugin but enabling post formats should probably go into your functions.php. All 3 of my examples could easily go into the other file though, so that’s where the debate lies. It’s your choice what goes where but if in doubt add it to the functionality plugin.

How to make a Functionality Plugin

Now you know why you should have a functionality plugin it’s time to create one for your self. You will be pleased to hear that, after reading all the info on it, creating and enabling the plugin is easy: simply copy/paste the following into an empty file named custom-func.php or something similar, the filename doesn’t really matter.

<?php
/*
Plugin Name: Custom Functionality Plugin
Plugin URI: http://www.pattonwebz.com/
Description: Moves most functions away from functions.php
Author: William Patton
Version: 1.0
Author URI: http://www.pattonwebz.com
License: GPL2
*/
?>

[further opening=”Download this as a ready to use plugin file: ” link=”http://www.pattonwebz.com/wp-content/uploads/2012/08/custom-func.zip”]Custom Functionality Plugin Template[/further]

If you place the file containing that code into a folder, upload it to your site and enable it you will have your own custom plugin running – congratulations!

As all the plugin contains is the comments listed above it doesn’t actually do anything but it’s ready for you to pop your code into and have it run. I have added a new custom functionality plugin to this very site while in the process of writing this article. The only custom function I have added to the new plugin is a shortcode that I have had in my head for a few days, and coded specifically for this article while I was writing it to demonstrate the usefulness of having your own functionality plugin.

The new shortcode creates a simple css box that accepts 2 parameters and it’s content, you can see it in action just below our stock custom plugin code above. The purpose of the box is primarily as a ‘further reading’ box but I have included a method of changing the opening text so I can use it for other purposes, as I have done above.

Hopefully you now have your own site specific custom functionality plugin running on your own site. If you have any problems don’t hesitate to ask me in the comments below and if you find this post helpful please use the share buttons below to share it, thanks.