I am creating a simple framework with theme options. I have divided chunks of code within functions.php
and placed it within a specific folder structure.
Now in my main functions.php
file, I only have require_once
calls to these files.
But for the sake of argument – let’s say that I’ll end up with 20 files to include.
QUESTIONS:
- Does this have effect on WP performance in a visible way ?
- Is it better to keep it all within 1 file (functions.php)
- what is the best way to go about this ?
Thanks.
IF it would have a real affect for some small files, then it would have an affect that has an impact lower than WP: PHP and server performance. Does it really have an affect? Not really. But you can still simply start doing performance tests yourself.
Now the question is “What is better”? From overall file(s) loading time? From file organisation point of view? Anyway, it doesn’t make any difference. Do it in a way so you don’t loose overview and can maintain the result in a way that is pleasant for you.
What I normally do is simply hooking somewhere in (
plugins_loaded
,after_setup_theme
, etc. – depends on what you need) and then just require them all:Anyway, you can make it a little more complicated and flexible as well. Take a look at that example:
It’s a class that does basically the same (needs PHP 5.3+). The benefit is that it’s a bit more fine grained, so you can easily just load files from folders that you need to perform a specific task:
Update
As we live in a new, post PHP v5.2 world, we can make use of the
FilterIterator
. Example of the shortest variant:If you have to stick with PHP v5.2, then you still can go with the
DirectoryIterator
and pretty much the same code.I reworked @kaiser answer a bit to my needs – thought I share it. I wanted more options, those are explained inside the code and at the usage example below.
Code:
Usage example: