Most of the WordPress tutorials I write mention adding custom functions to your theme’s functions.php file. Many of the other tutorials around the Web will use this same technique for adding custom code. However, there are different ways of handling custom functions.
Telling a user to drop code in their theme’s functions.php file makes things easy for both the developer and the user. It’s easy to understand because nearly every theme has this file and users are accustomed to editing their theme rather than dealing with plugin code.
This tutorial will walk you through two plugin alternatives to using a theme functions file.
The big problem with editing themes
Before moving forward, you should know that there’s nothing wrong with adding custom code to a theme’s functions.php file, especially if the code is directly related to the theme you’re using. That’s not a problem at all.
The problem occurs when you add custom functions that aren’t necessarily tied to a theme such as when creating custom post types, taxonomies, or shortcodes. Most of the time, you’d want to keep these types of things separate from your theme because you’d like to retain the functionality when you switch themes.
Of course, you can transfer one theme’s functions.php code over to another theme, but why not make it even easier, requiring no transfer of code?
Creating a custom functions plugin
For the two methods described in this tutorial, you will need to create a custom functions plugin. This plugin will house all of your custom functions. Before doing anything else, create a file called my-custom-functions.php on your computer. Place the following code in this file.
<?php
/**
* Plugin Name: My Custom Functions
* Plugin URI: http://yoursite.com
* Description: This is an awesome custom plugin with functionality that I'd like to keep when switching things.
* Author: Your Name
* Author URI: http://yoursite.com
* Version: 0.1.0
*/
/* Place custom code below this line. */
/* Place custom code above this line. */
?>
You can change the information at the top of this file to suit you. I prefer to leave the name as My Custom Functions so that I know it holds stuff that I’ve created.
Option #1: “Must-Use” plugin
Not many WordPress users are aware of what’s called the mu-plugins folder. WordPress doesn’t even ship with this folder by default, so it’s a bit of a hidden gem that I wish more folks knew about. Traditionally, it’s been thought of as part of the multi-site (old WordPress MU) setup by many people, but this folder works for single-site installs too.
The “mu” in the mu-plugins folder name stands for “must use.” What this means is that any plugins in this folder automatically run on all sites (single site or all sites on a multi-site install). There’s no need to activate them. They just run. This should feel familiar to those of you that are used to having code that just runs when adding it to your theme’s functions.php.
If you don’t already have a must-use plugins folder, go to the wp-content directory on your WordPress install and create a sub-directory named mu-plugins. Then, add your my-custom-functions.php file to this directory. Your directory should look like the following:
/wp-content
/mu-plugins
/my-custom-functions.php
If you visit your plugins page in the WordPress admin, you should see a new option named “Must Use” alongside your active and inactive plugins as shown in the following screenshot.

As shown in the screenshot, there’s no “activate” link for the plugin. It’s already running on your site since it’s in the mu-plugins directory.
The only downside to using this option is that you can’t deactivate the plugin without manually removing the file from your site using FTP or whatever other method you use for adding/removing files from your site. If this worries you, follow along for another option.
Option #2: Regular plugin
If you’d rather have a plugin that you can activate/deactivate, you can create a regular plugin. The process is much the same as in the previous step. Rather than adding your plugin to the mu-plugins folder, you would add your my-custom-functions.php file to the plugins folder in the wp-content directory.
Your file and directory structure would look like the following:
/wp-content
/plugins
/my-custom-functions.php
If you visit the plugins screen in the WordPress admin, you’ll see My Custom Functions listed in your regular list of plugins as shown in the following screenshot.

Remember, this will behave like a regular plugin, so it won’t automatically run like a must-use plugin. You still have to activate it.
The biggest downside to this option is that the plugin is mixed in with all your other plugins. I prefer the separation of the mu-plugins folder for things that I plan to always have running on my sites.
Get started with your custom functions plugin
Every time you read a tutorial with something you’d like to do in WordPress, stop and think a moment before implementing its code if it asks you to add it to your theme’s functions.php file. Ask yourself the following questions.
- Is this something I only want to happen when using my current theme?
- Is this functionality I want to keep regardless of the theme I’m using?
If you can answer those two questions, you’ll know where the code needs to go.
“it’s a bit of a hidden gem that I wish more folks knew about”
Me too! +1000 for this. It’s a handy thing.
Also! For those devs worried about clients disabling plugin on them – the use of this folder prevents that.
That’s definitely a good idea — keeping clients from disabling stuff that runs their site.
This is a really good way to tweak functions.php file, thanks for sharing!
Great post Justin! Had no idea about the mu-plugins folder. I’d seen it before with WPMU installs but no idea the mu stood for something totally different. Very handy trick, especially for making sure clients can’t deactivate something important on their site, thanks!
So cool, I haven’t ear about mu-plugin folder before reading this here, thanks !
I like this idea. A LOT. I appreciate the tip about adding the must-use plugins folder, too. Thank you!
Wow, here I was thinking that the mu-plugins folder only applied to multisite sites. That’s the best WordPress tip I’ve learnt in ages. Thanks for the post Justin!
As with others, I had no idea that MU stood for must-use, instead of WPMU.
Great ideas.
Great tip. I can see how this would be very handy. I like the MU folder option too. Is this all that needs to be done for it to work or does some code need to be entered in the regular functions.php file to call the custom functions?
All that needs to be done is in the post. There’s no need to do anything with your theme’s
functions.php. You use this just like any other plugin.Like others who have commented, I too had no idea about the “must use” plugin dir.
It would be interesting to see the code you have in yours – ever considered putting it up for us to look at?
Thanks for the post.
Mostly, I use mine for custom post types, taxonomies, shortcodes, and stuff related to change the posts query on specific pages. It also depends on the site because I manage several different sites.
great idea
I was still using the my-hacks file (I know it’s deprecated). I change now. Thanks for this tips.
Old school.
this is very cool indeed. thanks for the tips.
so if it’s in the mu folder, there is no disable / delete link?
what if I modify an existing plugin and don’t want the client to update it, can I put it there instead of using this :
http://markjaquith.wordpress.com/2009/12/14/excluding-your-plugin-or-theme-from-update-checks/
Nope, there’s no deactivate or delete link when using the
mu-pluginsfolder. Think of it as something that your site must use to run.I don’t think updates will work. You can’t add an entire plugin folder (it has to be a single file) in it anyway.
Justin, I thought in 3.1 you now CAN add an entire folder into the MU-PLUGINs folder? Am I mistaken? You can add a single file that pulls in other files using includes also only pulling them in when they’re needed to increase the speed of the site.
I don’t think so. This came up in ticket #14359, which was closed as wontfix.
First time to hear… Again, from you…
Ditto on the coolness of learning about mu-plugins!
One thing is that my thinking about custom code kind of goes the other way round. I think, “If this code is portable, and I could use it on other sites, then [ideally!] I’ll make it into a plugin.” But stuff like custom post types and taxonomies – unless, of course, you’re creating a fully-fledged “management” plugin with a UI – is surely site-specific, and therefore is best as part of the theme’s functions.php.
Of course, I’m thinking here of the bulk of my clients, who get me to create a completely custom theme for them. And of course, there’s nothing necessarily bad against creating plugins just for one site…
Custom work is sometimes different. I would most likely discuss options with the client before adding things like custom post types and taxonomies to the theme. Then, just take it from there. I tend to lean toward using a plugin for things they’ll have on their site in the long run.
I can’t believe I have been creating themes this long and had no idea this folder could be created. Thanks a million!
Q: If you place a public plugin, like from the wp directory, will wp still notify when updates are needed? Or is it best to only use your own “custom” functions in the mu folder?
I don’t think updates will work, but I’ve also never tried. You can’t add an entire plugin folder to the file though. It has to be a single file.
Try here: http://w-shadow.com/blog/2010/09/02/automatic-updates-for-any-plugin/
I have been chucking all my useful functions into a plugin file regularly for a couple years now.
Good post!
Great tip, and will definitely be using. I’m an old Mu user, and never even thought of this, but didn’t realize you could use on single install too. Kudos!!
One important thing to note is that unlike the normal plugins folder, you cannot place must-use plugins in a subfolder. They have to be directly in the mu-plugins folder, or they will not work or show up in the plugins list.
There are also a couple of handy constants available for use in your must-use plugins: WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL.
Those are definitely great tips to keep in mind.
Thanks Simon! I forgot about the sub-folders. you may have just saved me a headache. -J
A very timely post Justin. I was just at WordCamp Phoenix and attended a session about the creation and use of Custom Post Types. I asked if there were plugins that could be used to create and manage CPTs (and Custom Meta Boxes) and was told about one created by Brad Williams…but I was strongly encouraged to NOT use a plugin for CPTs.
I’m very familiar with adding functions to my functions.php file, and making these into a portable plugin I can use seems like a good comprimise.
Thanks for writing this up!
***feature creep*** – Do you think it would be a valid idea to take this further and create a GUI for people to either write or paste in their custom functions? Then output a list of all the functions to the admin and give them the option to turn them on and off?
Brad Williams may not like my answer.
But, I don’t think custom post type UI plugins are a good thing. Custom post types are about implementation, not registration. A plugin that manages this can only focus on registration. It does nothing for the implementation. So, what’s the point of using a plugin to handle 20 lines of code since you’re going to be implementing everything else anyway?
Sure, if you’ll be creating something extremely similar to blog posts or pages, you could use a custom post type management plugin. But, maybe you should consider just creating posts or pages instead of a custom post type in this case.
As for a UI for people to input PHP, it’s dangerous territory (I’ve even done something similar before). The problem: people should be comfortable enough writing PHP to use a functions file if they’re going to be adding PHP to their site.
Woohoo, a hidden gem indeed!
Thanks Justin.
I already knew about drop-ins and I’ve used it for a couple of clients’ sites, but MU’s were “multi-user” to me until today
A good idea when you design a site for a half-tech-half-noob client.
Hi, nice tip. What hook should we use here to call our functions? They are not anymore theme related so maybe there is no need to wait for ‘after_setup_theme’?
The question is a bit too general. It depends on what the function does and when it should execute within the WordPress flow.
Right. Maybe you can recommend some resources to understand how to choose the proper hook depending on the plugin function.
I found out that I need to split what I put in the theme’s `function.php` file into several of those plugins for the sake of consistency and reusability, and they deal with some custom post types, some frontend editor, a points based rewarding system and some extended user management functions (additional metadata definition, etc).
The action hook and filter hook references on the Codex are a good place to start. Of course, we cover this kind of thing in our upcoming book, Pro WP Plugin Development.
Thanks Justin. This is deceptively simple. I’ve been thinking for some time (as I’m sure others have) about how to do this in a ‘clean’ fashion. This is just perfect. And again, deceptively simple. Thanks for pointing it out!
-Joshua Guffey
Didn’t know that mu-plugins folder works with single site too. Thanks for the post Justin!
I ended up doing this after some comments from Otto quite a while back. He was suggesting creating a series of itty bitty plugins for each little thing that you need, that way it’s simply a matter of copying the individual plugins you need over to any other sites you may be working on. This has now resulted in me having dozens of tiny plugins running on my sites, but it’s pretty handy having all that code extracted out of a single chunk of code so that I can easily migrate them elsewhere when I need to. It doesn’t work so well on clients sites though as it’s a bit confusing having so many different (and usually poorly documented) plugins scattered around their install, so I still tend to use functions.php in that situation.
I’ve been doing this for a while. I was always wondering if it was a good idea or not. Now I am much more confident in my approach! I think using the theme functions.php file for anything other than appearance related features is a very bad idea.
Good article. I have a question. Do you know of a WP plug-in that will tell me how many hits a post gets on a blog?
hi
please could you tell me could we modify complete wordpress, each and every themes will change to custom template and all files rename and it will looks custom developed script.
can we do this ???? and is it possible.
thanks
Linda
Like (apparently) a lot of folks, I knew mu-plugins from multisites, but had no idea that it could be used the same way for single installs. Adding it to the bag of tricks – thanks!
You make a great point about the functions.php file. All too often some developers use that file in the theme to make major changes to the site and starts to get some sloppy nested loops or calls and slows down the entire site. This is a great way to handle some updating problems. Thanks
Usefulness, thanks!
I’m using it now to hide dashboard widgets and hide menus from the client, so they don’t break their site
Brilliant. I’d already moved the functions in one site’s functions.php to a plugin file (the Family Tree web site which your custom taxonomies/post types tutorials have already helped with) because I use a different template for mobile visitors but the mu-plugins directory could come in very handy with clients who insist on having admin rights… I can just hide the plugins somewhere where they can’t delete them :->
Nice stuff! Are there any advantages of using this approach versus building a custom framework and including it via the functions.php, similar to what we do with Hybrid Core?
Very slick Justin, never even thought about using /mu-plugins – for non multi-site.
Great article.
Great post Justin. Thanks a lot.
NB: I haven’t find any share button in this page to share this article.
Good information. Two years ago i try fix my own blog, but i do something wrong and die!
Now leave that job to a programmer.
Thanks for the excellent mu-plugin tip!
I’ve been looking for a way to accomplish this very thing, and your insightful tip has provided me the solution that I was looking for.
@ Justin – this is probably a wrong place to write this. But u are amazing. I have found ur presence across the internet on various places. Really respect your designing abilities.
i have also come across some of these problems when editing themes iam going to use you method for custom plugins
thanks ! i think with wordpress we jusst need a function file to do all of theme !
That is awesome… Thanks for the tips… Very helpful indeed! It’s worth the read!
Wow, it’s shocking.. its the first time I read about it, I did never know this WordPress hidden treasure are exists.
Thanks for the great tips..
Helpful tutorial. I want to create my own custom plugins for social bookmarking sidebar widget. And now I can do it with the help of this article. Will share my custom plugin soon. So, Thanks a lot.
Once I tried to do this, and failed. It seemed logical to me that custom post types and other non-theme options should go into file that has nothing to do with theme. So I created plugin and something went wrong. So, I believed that that is impossible to do. Thanks for this, I have just made what I wanted a year ago
Fantastic post.
Please keep up the great work you do. I’ve referred back to your posts dozens of times in the past few months.
Terry
Hey Justin,
Thanks a lot. This was an awesome tutorial. Bookmark and Subscribe
Never heard of the must use fuctionality before but I’m going to try it out.
Fantastic. This has saved writing Child themes for those sites that only required a change or addition to the functions.php file.
Thank you!
Justin,
Thanks for posting this. I didn’t know this existed. This is great for automating WordPress installs that use custom plugins for branded CMS purposes.
Great if you have control of the theme, but you would need to be careful for distributed themes.
If the file called a function on load from a theme or a plugin, the developer did not code to see if it existed and escaped, and the theme or plugin was disabled would this be a fatal admin lockout?
What I do at the moment with 2010 child themes is have a folder of smaller function files to be included, and have a function in functions.php to read all files in the folder onload.
So I have, category-functions.php, meta-functions.php, facebook-functions.php, post-functions.php with a few theme specific calls in each, it is very easy to manage, I add one function in functions.php and it reads any file in the folder in.
Thinking about it, it could be nice if WordPress looked for a ‘theme-mu’ folder and loaded any files into functions.php on run.
This would mean functions could be split out into smaller targetted different functions files by developers.
David
Great tip. I’ve never heard about the mu-folder. From this day on, things will change!
Thanks.
Never in 1000 years I would have thought that mu stands for MustUse!!
Thanks so much, you’ve changed my life forever!
Very useful tip. This makes adding code even more confinement.
You can just upload the custom code plugin the normal way via wordpress plugin upload function, test it and when all is well, move it to the mu-plugin folder.
Thanks for this very well done tip!
Your info about the mu-plugins folder is the useful tip on WP useage I have seen in ages. Thanks mate.
Nice… I’ll use this
I’ve seen the mu-plugins folder before but didn’t realise that it stood for must use! Thanks for sharing this information, will definitely be putting it to use for my functionality plugins in future.
Man this rocks, thanks Justin!
It sure makes migrating function from theme to theme without adding too much to each functions file.
i saw this code but didn’t knew what it was… thanks !
Great. It works. And it is extremy useful.
“mu-plugins” will be my favorite folder!
Hi Justin,
Great advice on making custom modifications! I recently downloaded your feature a page widget and got into the read me file to make the “read more…” mod and haven’t been able to succeed. I’m not sure if it’s due to the theme I’m using, Suffusion. I initially updated the functions.php with no luck and then created the mu-plugins folder per this post with the my-custom-functions.php from the code in the read me doc of the plugin and still no luck. Just wondering if you could point me in the right direction
)
Thanks!
Stephanie
Thank you! Been using WP for years and Thesis “custom-function.php” but moving away from that to our own in-house framework. This is perfect!
I have tried both methods, both report the same errors
“The plugin generated 674 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin”
any ideas, what have I don’t wrong?
oops, It’s OK error in copy & paste, doh!
Actually I was looking for how to create custom post types. And now I got what is mu-plugins means. Its better using a plugin when creating custom post types than functions.php. I decided to make a plugin (mu-plugins). I asked what if I want to use an another theme? Better way is letting to hold the daha custom post type plugin…