The more customization I make to WordPress the more I start thinking about if I should be organizing this file or splitting it up.
More specifically, if I have a bunch of custom functions which only apply to the admin area and others which just apply to my public website is there any reason to possibly include all admin functions within their own file or group them together?
Would splitting them up into separate files or grouping them together possibly speed up a WordPress website or does WordPress/PHP automatically skip over functions which have an is_admin code prefix?
What’s the best way to deal with a large functions file (mine is 1370 lines long).
If you are getting to the point where the code in your theme’s
functions.php
is starting to overwhelm you I would definitely say you are ready to consider splitting it up into multiple files. I tend to do that almost by second nature at this point.Use Include Files in your Theme’s
functions.php
FileI create a subdirectory called “includes” under my theme directory and segment my code into include files organized by what makes sense to me at the time (which means I’m constantly refactoring and moving code around as a site evolves.) I also rarely put any real code in
functions.php
; everything goes in the include files; just my preference.Just to give you an example here’s my test install that I use to test my answers to questions here on WordPress Answers. Every time I answer a question I keep the code around in case I need it again. This isn’t exactly what you’ll do for a live site but it shows the mechanics of splitting up the code:
Or Create Plugins
Another option it to start grouping your code by function and create your own plugins. For me I start coding in the theme’s
functions.php
file and by the time I get the code fleshed out I’ve moved most of my code into plugins.However NO Significant Performance Gain From PHP Code Organization
On the other hand structuring your PHP files is 99% about creating order and maintainability and 1% about performance, if that (organizing
.js
and.css
files called by the browser via HTTP is a completely different case and has huge performance implications.) But how you organize your PHP code on the server pretty much doesn’t matter from a performance perspective.And Code Organization is Personal Preference
And last but not least code organization is personal preference. Some people would hate how I organize code just as I might hate how they do it too. Find something you like and stick with it, but allow your strategy to evolve over time as you learn more and get more comfortable with it.
Late answer
How to include your files the right way:
The same works in plugins too.
How to get the right path or URi
Also take a look at file system API functions like:
home_url()
plugin_dir_url()
plugin_dir_path()
admin_url()
get_template_directory()
get_template_directory_uri()
get_stylesheet_directory()
get_stylesheet_directory_uri()
How to reduce the number of
include/require
If you need to fetch all files from a directory go with
Keep in mind that this ignores failures (maybe good for production use)/not loadable files.
To alter this behavior you might want to use a different config during development:
Edit: OOP/SPL approach
As I just came back and saw that this answer is getting more and more upvotes, I thought I might show how I’m doing it nowadays – in a PHP 5.3+ world. The following example loads all files from a themes subfolder named
src/
. This is where I have my libraries that handle certain tasks like menus, images, etc. You don’t even have to care about the name as every single file gets loaded. If you have other subfolders in this directory, they get ignored.The
FilesystemIterator
is the PHP 5.3+ supercedor over theDirectoryIterator
. Both are part of the PHP SPL. While PHP 5.2 made it possible to turn the built in SPL extension off (below 1% of all installs did that), the SPL now is part of PHP core.Previously while I still supported PHP 5.2.x, I used the following solution: A
FilterIterator
in thesrc/Filters
directory to only retrieve files (and not dot pointers of folders) and aDirectoryIterator
to do the looping and loading.The
FilterIterator
was as easy as that:Aside from PHP 5.2 being dead/EOL by now (and 5.3 as well), there’s the fact that it’s more code and one more file in the game, so there’s no reason to go with the later and support PHP 5.2.x.
Summed up
EDIT The obviously correct way is to use
namespace
d code, prepared for PSR-4 autoloading by putting everything in the appropriate directory that already is defined via the namespace. Then just use Composer and acomposer.json
to manage your dependencies and let it auto-build your PHP autoloader (that imports automatically a file by just callinguse <namespace>ClassName
). That’s the de-facto standard in the PHP world, the easiest way to go and even more pre-automated and simplified by WP Starter.I like to use a function to the files inside a folder. This approach makes it easy to add new features when adding new files. But I write always in class or with namespaces – give it more control about the Namespace of functions, method etc.
Below a small example; ut also useage with the agreement about the class*.php
In Themes I use often a other scenario. I define the function of the externel file in a support ID, see the example. That is usefull if I will easy deactivate the feture of the externel file. I use the WP core function
require_if_theme_supports()
and he load only, if the support ID was active. In the follow example I deifned this supported ID in the line before load the file.You can see more of this in the repo of this theme.
in terms of breaking it up, in my boiler plate I use a custom function to look for a folder called functions in the theme directory, if it is not there it creates it. Then is creates an array of all the .php files it finds in that folder (if any) and runs an include(); on each of them.
That way, each time I need to write some new functionality, I just add a PHP file to the functions folder, and don’t have to worry about coding it into the site.
I manage a site with about 50 unique custom page types in serveral different languages over a network installation. Along with a TON of plugins.
We where forced to split it all up at some point. A functions file with 20-30k lines of code is not funny at all.
We decided to completley refactor all code in order to manage the codebase better. The default wordpress theme structure is good for small sites, but not for bigger sites.
Our new functions.php only contains what is necessary to start the site, but nothing which belongs to a specific page.
The theme layout we use now is similar to the MCV design pattern, but in a procedural coding style.
For example our member page:
page-member.php. Responsible for initializing the page. Calling the correct ajax functions or similar. Could be equivialent to the Controller part in the MCV style.
functions-member.php. Contains all functions related to this page. This is also included in serveral other pages which need functions for our members.
content-member.php. Prepares the data for HTML Could be equivialent to the Model in MCV.
layout-member.php. The HTML part.
Efter we did these changes the development time have easily dropped by 50% and now the product owner have trouble giving us new tasks. 🙂
From child themes functions.php file:
In functions.php, a more elegant way to call a required file would be:
require_once locate_template(‘/inc/functions/shortcodes.php’);
I combined @kaiser‘s and @mikeschinkel‘s answers.
I have all my customizations to my theme in a
/includes
folder and within that folder I have everything broken out into sub folders.I only want
/includes/admin
and its sub-contents to be included whentrue === is_admin()
If a folder is excluded in
iterator_check_traversal_callback
by returningfalse
then its sub-directories will not be iterated (or passed toiterator_check_traversal_callback
)