Functionality Plugins remove the expiration date of your functions

Create a Functionality Plugin Instead of Using Functions.php

| 27 Comments

If you find this tutorial too difficult to follow, you can hire me to build your functionality plugin to suit your specific needs.

A few months ago, I read a post on WPCandy which discussed the idea of creating a functionality plugin. I’d never heard of this before, but I was intrigued, so I read on.

By the time I was finished with the post, I was a total convert and was dumbfounded that we’d been doing things differently for so long. Now I’m going to help spread the word by telling you all about it…

What is a functionality plugin?

Traditionally, if you wanted to create a new function for your blog to add to its functionality, the advice has been to add the code snippet to your theme’s functions.php file. Now that’s all well and good and it will work fine, but what happens when you change your site’s theme (which you can guarantee will happen at least every few years) or the theme gets updated? Since functions.php is located in your active theme folder, if you change your theme, it inherently becomes defunct.

So, how do we get around it? In short, you create a plugin which will house all most of your functions. All plugins get loaded on every page, the same as your functions.php file, so it doesn’t matter whether the functions appear in your functions.php file or a (any) plugin.

Alright, I’m sold. How do I do it?

As with all plugins, you need to create a new folder in wp-content/plugins. I tend to call mine sitename-functionality-plugin, where sitename is the name of the site that the plugin is for. Inside that folder, you’re then going to create a file called sitename-functionality-plugin.php. Once it’s created, you can open it for editing.

If you’d rather work from a template, download and install this plugin that I created, then activate it and edit it from Plugins > Editor.

At the top of the file, you need to paste plugin information in order for WordPress to properly recognise the file. Use the following as a template, changing the information as is appropriate for your own site:

Once that’s in place, you’re free to edit the remainder of the file. So you can take any functions from your functions.php file and paste them below your plugin header. So for example, your functionality plugin might start to look like this:

Should I ever use functions.php?

Actually, yes. There will be select times when using functions.php is appropriate. Think of it this way; if the function is directly related to the theme or your site layout, it probably belongs in functions.php if that function would no longer be necessary if you changed your theme. An example of this might be a function that adds image sizes that are very specific to your theme, to fit into a slider for example.

However, other functions such as setting favicons for your site or creating bit.ly short URLs are independent of the theme, and you’ll want to use regardless of the theme that you’re using, so you should be putting them in your functionality plugin.

So, have you seen the light? Have you created your functionality plugin yet? Please help spread the word and share this article!

Categories: Code & Snippets, Plugins | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler

Author:

Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.

27 Comments

  1. Dave, that’s brilliant! Definitely putting this tip in the toolbox. I feel a lot better modifying this file than a core file in a theme that someone paid for and/or gets updated by the developer.

  2. Hi Dave,

    Does adding many functions in functions.php file slow down WordPress? I know installing many plugins is bad, but what about functions?

    • No, not at all. The reason you don’t want too many plugins is generally because of the number of HTTP requests they create. That’s what slows you down, rather than the number themselves. So you could have 25 plugins each with 1 HTTP request and that would likely be faster than a single plugin with 50 HTTP requests.

      That said, I usually prefer functions for small little things like this because you have full control over everything and don’t need to include HTTP requests if they’re not needed – you don’t typically check plugins for their coding efficiency, which is why installing loads of them can be detrimental.

      Thanks for your question.

  3. this post is actually great.. as a new blogger i must admit that i don’t have much knowledge regarding this matter but i am very much willing to learn more… thank you so much for sharing this one! i would love to check these out!

    • You’re quite welcome Chelle. Best to start off the right way than having to do a lot of rework! I hope you continue to learn and become a WordPress ninja in no time! Cheers.

  4. I hadn’t heard of this idea… looking back it’s brilliant (of course!). Really liked how you determine what to keep in functions.php (anything theme dependent). I’m off to build…

  5. This is great. I’ve been thrown off before by switching themes after creating custom post types and other functions. This solves it and makes perfect sense. Consider me a convert. (and thank you.)

  6. Hi Dave,
    There are a number of extra sidebars that I register in every theme I make. Can that be done by this plugin? Will it not clash with the normal register_sidebar in the functions.php file?
    Thanks

    • No, you can register multiple sidebars from several different theme files and plugins. Your function name will need to be unique of course, but you can use the register-sidebar hook multiple times.

  7. Hi Dave

    I arrived at this page after reading your ‘add anything to the end of posts’ page. Doing things this way make perfect sense as I’m always worried about loosing customisation when I change themes or make other changes. I’m going to experiment doing this first on an old blog in case I make a mess of it, as I’m not a coder.

  8. My question is why on earth would you not be using child themes in all cases? Any WP theme can have children and any customization, including adding functions should be in a child theme, because, like you said, if the theme is updated, then the functions.php file remains intact.

    Don’t get me wrong, this is cool. However, for this to even be all that practical means that I would not be using a child theme as my active theme which is a huge no-no.

    • I agree in using child themes in most cases. I have a tutorial on it and highly recommend it. However, I don’t see why you still wouldn’t use this. There are certain functions that belong in functions.php, and they should then go in your child theme’s functions.php. However, there are other functions which belong in a functionality plugin and there’s no reason why you couldn’t use a functionality plugin in conjunction with a child theme, even if it does also have a functions.php file.

  9. This is so cool, it’s kind of creepy. Made my day because I’m always goosey about changing a theme that someone else coded… and although I use child codes, I’ve never been able to get everything working in all the child files at the same time.

    So, great post!

    I do have one question…. in the .php file, are additional functions discrete items that are fenced with their own php delimiters, or is the entire file read as a series of functions and there’s only one set of php delimiters at the beginning and end of the file?

    Thanks again!

    • It’s up to you. I typically leave the PHP tags open for the whole file and just add functions as I need them, but it will work just as well if you open and close after every function (though a little less efficient).

  10. Thanks for sharing this. I’m in the process of trying to use something like this for adding google webmaster tools, bing, yahoo, etc….

  11. Hi that`s a nice alternative^^…but i prefer to create a mu-plugins folder inside of my wp-content folder…there you can create as many files as you want…so if you have a lot of hooks you can saperate them by special files

    cheers

  12. I am definitely going to start doing this more often. I like the idea of creating it as a must-use plugin (as Martin mentioned above)
    The beauty of the mu plugin is that it doesn’t need activating…and also clients cannot deactivate it and screw-up the site!

  13. Instead of “just” your functionality-plugin you can use one of these plugins
    http://wordpress.org/plugins/toolbox/
    http://wordpress.org/plugins/code-snippets/
    to add some extra features for your snippets organization (like loading just in frontend, backend or both).

  14. Hello,
    great idea!
    Is it possible to manage each function in the backend with button radio “yes” or “no”?

Leave a Reply