Late answer
How to include your files the right way:
function wpse1403_bootstrap()
{
// Here we load from our includes directory
// This considers parent and child themes as well
locate_template( array( 'inc/foo.class.php' ), true, true );
}
add_action( 'after_setup_theme', 'wpse1403_bootstrap' );
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()
- etc.
How to reduce the number of include/require
If you need to fetch all files from a directory go with
foreach ( glob( 'path/to/folder/*.php' ) as $file )
include $file;
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:
$files = ( defined( 'WP_DEBUG' ) AND WP_DEBUG )
? glob( 'path/to/folder/*.php', GLOB_ERR )
: glob( 'path/to/folder/*.php' )
foreach ( $files as $file )
include $file;
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 the \DirectoryIterator
. 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.
<?php
namespace Theme;
$files = new \FilesystemIterator( __DIR__.'/src', \FilesystemIterator::SKIP_DOTS );
foreach ( $files as $file )
{
/** @noinspection PhpIncludeInspection */
! $files->isDir() and include $files->getRealPath();
}
Previously while I still supported PHP 5.2.x, I used the following solution: A \FilterIterator
in the src/Filters
directory to only retrieve files (and not dot pointers of folders) and a \DirectoryIterator
to do the looping and loading.
namespace Theme;
use Theme\Filters\IncludesFilter;
$files = new IncludesFilter( new \DirectoryIterator( __DIR__.'/src' ) );
foreach ( $files as $file )
{
include_once $files->current()->getRealPath();
}
The \FilterIterator
was as easy as that:
<?php
namespace Theme\Filters;
class IncludesFilter extends \FilterIterator
{
public function accept()
{
return
! $this->current()->isDot()
and $this->current()->isFile()
and $this->current()->isReadable();
}
}
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 a composer.json
to manage your dependencies and let it auto-build your PHP autoloader (that imports automatically a file by just calling use \<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.