WordPress in the Enterprise – The Functions File

We covered the topic of linking your company’s proprietary code library to WordPress in an earlier article, but those classes are generally for high level services, integrations and multi-project utility classes only. Examples of classes you might find there would be a class for making Application Programming Interface (API) calls to a customer relational management (CRM) system, a system logger or event classes for validation of data like emails and URLs. In this article we will expand on that earlier idea and talk about linking in classes and functions that are more theme specific. Classes that may encompass other enterprise level functionality you do not want to shared with other projects or even other themes.

The Themes File “Functions.php”

Undoubtedly you have heard of the functions.php file if you have been in WordPress for any length of time. Perhaps you wanted to hook up some custom code and went looking on the Internet to learn how to do it. If you did, you would have found someone somewhere saying that you need to put your code in the functions.php file. What may not be clear is that this file is typically for a given theme. Change the theme and a different functions.php is loaded. This is not typically a problem if you have a theme you like, because how often do you change it?

The functions file can get pretty popular as you can imagine. You will find yourself dumping in action hook code, filters, shortcodes and helper functions that you might call in your theme. You might also want to put in some classes just for use by a given theme. An example might be a class that talks to Twitter for feeding a Twitter widget. This type of code can lead to an extremely long and unorganized file over time. It might be worth putting it into its own plugin, but that can be a bit heavy handed. So let’s talk about another way we can solve this problem of functions.php clutter.

Who Says You Can’t Use Includes in Functions.php?

If you speak to most programmers they will tell you about the practice of one class per file. It is a good habit to get into. If you take this concept and expand on it for WordPress, you could also make a file just for actions and one just for filters and one file for each PHP class you develop for the theme. These files can make up a library of theme specific code that will keep things organized and will clean up your busy functions.php file dramatically. Paired with my earlier article on building in a general class library, you will develop a two level library system. One for general classes that can be shared across projects that your WordPress installation can use to tie into existing enterprise systems and then this one where you store more specialized code just for this theme.

I start by creating a “lib” folder in the theme folder of the given theme. Here you can have an actions.php file where you post all action related code. Another file I typically call filters.php would contain all your filters. Then of course you have each of your classes in their own file and I am sure you get the picture. While we spoke in the previous article about building in a class loader for our general classes, you could opt for a a class loader here as well. It is really up to you.

The next step would be to include those files in functions.php. Using the include statement you just add the lines like…

include 'lib/actions.php';
include 'lib/filters.php';
include 'lib/class.TwitterClient.php';

Again this is only necessary if you decided not to go the autoloader route I mentioned before. I suggest you also watch your ordering here to make sure that if you have any code that relies on another include be towards the bottom of the list.

You could also use the same approach for including a file that contains defined constants. I like to use a file of constants to help with configuration settings. I setup a file called “config.php” and include it into the functions.php preferably at the top of the list so other files can see the constants. Once included, the variables and other includes will then be available to your theme.

Some Dangers To Watch Out For

One of the major caveats to this technique is that it may encourage you to be reckless on using your new found freedom. You might be inspired to use your functions in plugins or other areas of the site that is not theme specific. This would build in dependencies that lock you into your given theme. If you find code that you may want to reuse at a higher level, consider putting it up into the higher level class library we created earlier.

You always want to be looking out for any possibility that two pieces of code are depending on each other when they shouldn’t. If you build in dependencies that may later be broken, you might want to consider using class injection to help alleviate some of that inter-dependency.


Between this article and my previous article “WordPress for the Enterprise – Building in a Class Library” you have a rich setup of classes that can be easily expanded and scoped to the particular work you are doing. You also learned how you can link more specific theme related code into your functions.php file to reduce clutter and provide you the flexibility to alter WordPress in ways that you haven’t previously considered. Lastly, you learned about the main danger of such a setup and how it might be wise for you to keep an eye out for any type of dependencies between your code files or code you might use elsewhere besides themes.

I have used this setup in an enterprise grade setup and has proven widely successful. It involved integrating WordPress with several 3rd party systems and created a framework for expanding the company’s installation in ways they have yet to realize. They have been excited to continue growing out their web presence and already have plans for future integrations with other services. I am just glad this setup gives me the chance to say to them “Yeah, no problem. We can do that.”

If you liked this article and want to cut loose your new found skills on some projects, please consider downloading our ebook “The Programmers Idea Book” which features 200 programming ideas in several categories including files, networking, databases and more. Each contains expert tips on how to think about the idea and resources to get you started. Each idea is also rated for difficulty so you won’t get in over your head.

Thank you for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.