Create a Functionality Plugin Instead of Using Functions.php

Functionality Plugins remove the expiration date of your functions
The information in this article is still correct, but I gave a presentation at 2014 WordCamp Tampa with an even better way of doing things, so you may want to check out Putting things where they belong, which includes templates for you to start from and examples to show you how to implement changes.

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.

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 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!

At 2014 WordCamp Tampa, I gave a presentation about how to use child themes and functionality plugins to better organise your code. It goes into great depth, with lots of slides, examples and templates for your child theme and functionality plugin to get you off on the right foot. Check it out: Putting things where they belong

35 thoughts on “Create a Functionality Plugin Instead of Using Functions.php”

  1. MaAnna says:

    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.

    1. Thanks MaAnna! I’m glad you see it that way and hope you’re able to influence a few people at the same time!

  2. Renji says:

    Hi Dave,

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

    1. 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.

      1. Renji says:

        Same here, I prefer functions too. Thanks for that nice explanation. :)

  3. chelle says:

    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!

    1. 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. Mercer says:

    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…

    1. Thanks Mercer. I’m glad you found it so useful. Hope you help to spread the word

  5. Evan Payne says:

    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.)

    1. Thanks Evan, glad to see another convert!

  6. jzigbe says:

    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?

    1. 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. Beth says:

    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.

    1. Absolutely. I encourage you to give it a go. Give me a shout here or on Twitter if you encounter any problems. I’d be glad to help.

  8. Brentini says:

    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.

    1. 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. Art says:

    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!

    1. 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. Noah's Dad says:

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

    1. Well, you should go for it! Only takes a few minutes to set it up, and then you’re away.

  11. martin says:

    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


    1. Well, that’s an option too, yes. Not sure that it has any advantages over this way or not; so perhaps it’s just a different way.

  12. John says:

    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. Torsten says:

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

  14. oWEB says:

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

    1. You’d have to create an options page, but sure, anything’s possible.

  15. Rick says:

    Late to the party here, but I have been spending google-hours trying to figure out this:

    How do I fully replace a function in the WP Core? Specifically, I want to use my own comment_form() function, totally replacing the comment_form() in the WP core. The things that I want to do are not available via hooks. I have tried using add_filter(‘comment_corm’, ‘my_comment_form’), but my_comment_form() function never gets called; the core comment_form() is used.

    Obviously, don’t want to modify core code. And don’t want to do it in a custom/child theme. So I am going the plugin route.


    1. Rick, there’s a whole bunch of filters for the comment form. Justin Tadlock has a great write-up on modifying the comment form. Scroll down to the “Filtering the comment form defaults” section.

  16. Rick says:

    Dave: I am aware of the existing comment filters. But there are none that allow me to change the action of the form, for instance. Or replace the entire comment_form() function with my own. And those are two of the things that I need to do.

    So, still looking for that technique: replacing the entire comment_form() function.


    1. Well, is there a good reason you can’t use a child theme (maybe you’re using Genesis)? That would be the usual way of doing it.

  17. Rick says:

    Dave: I wish to use a plugin that can be used with most (compliant to standard) themes. The plugin will replace the form section of the comment with additional code that helps block form spammers.

    So the plugin code needs to replace the comment code. Since there is no hook available for the entire comment_form() function, I can use the ‘core’ comment template code, then modify it how I want, then the modified file becomes the code used by all comment functions. I am having trouble with the commands to replace the theme (or the core, if the theme doesn’t have a comment template) comment.php (I believe that is the file I need to replace).

    I have done some child themes, but I want my plugin to work with most themes.

    1. I’m not sure entirely, but I’d start by looking at how other plugins do it. The first one that came to mind is Jetpack, and you can see how they unhook all existing comment form actions and then load in their own comment form.

      Another one that I thought of that brings in its own comment form is Disqus, so it might be worthwhile seeing how they do it. I’d be interested to hear if they’ve taken another approach.

  18. Anthony C. says:

    Can I call multiple functions from different plugins to achieve a larger project? My challenge is I’m working with a large operation built off gravity forms and it’s getting extensive. I’d like to keep them more managed by calling smaller files, but right now everything is built off the functions.php. I’m looking into the functionality plugin, but I’m also looking for a better way to manage the operation too. Glad to see this post is still running.

    1. Yes, absolutely. A function declared in one plugin is available to all plugins (unless you explicitly make it private). When developing the plugins though, you should make use of the if( function_exists() ) function, so that if one plugin accidentally gets deactivated or corrupted, it’s not trying to run a function from a different function that hasn’t been declared (since the plugin isn’t active).

Leave a Reply