Create a Functionality Plugin Instead of Using Functions.php

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.

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

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.

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!

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

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.

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.

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?

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

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

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!

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.

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.

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.

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.

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

Wish your site was as fast as this?

Do It With WordPress is proudly hosted by WP Engine, the very best WordPress hosting that money can buy.

Aside from being blazing fast, it's very secure, has a staging area to test changes before making them live, automated daily backups, malware scanning, stellar support from WordPress experts... It's just everything that you would want from your host, and more.