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
I 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 . 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:
<?php /* * functions.php * */ require_once( __DIR__ . '/includes/null-meta-compare.php'); require_once( __DIR__ . '/includes/older-examples.php'); require_once( __DIR__ . '/includes/wp-admin-menu-classes.php'); require_once( __DIR__ . '/includes/admin-menu-function-examples.php'); // WA: Adding a Taxonomy Filter to Admin List for a Custom Post Type? // http://wordpress.stackexchange.com/questions/578/ require_once( __DIR__ . '/includes/cpt-filtering-in-admin.php'); require_once( __DIR__ . '/includes/category-fields.php'); require_once( __DIR__ . '/includes/post-list-shortcode.php'); require_once( __DIR__ . '/includes/car-type-urls.php'); require_once( __DIR__ . '/includes/buffer-all.php'); require_once( __DIR__ . '/includes/get-page-selector.php'); // http://wordpress.stackexchange.com/questions/907/ require_once( __DIR__ . '/includes/top-5-posts-per-category.php'); // http://wordpress.stackexchange.com/questions/951/ require_once( __DIR__ . '/includes/alternate-category-metabox.php'); // http://lists.automattic.com/pipermail/wp-hackers/2010-August/034384.html require_once( __DIR__ . '/includes/remove-status.php'); // http://wordpress.stackexchange.com/questions/1027/removing-the-your-backup-folder-might-be-visible-to-the-public-message-generate require_once( __DIR__ . '/includes/301-redirects.php');
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
.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.