PHP files included in functions.php don't seem to work

This is my first post on WordPress StackExchange, I’ll try to be precise.

My question is: Is there anything in particular I should do to the files I’m including in my theme’s functions.php file?

I’m creating a WordPress theme and my functions.php file is getting very big now. I managed to remove a lot of code by turning some functions into plugins but it is still huge.

I would like to be able to organize the code with includes. I tried a couple of things but it didn’t work. It’s like the files I included in my functions.php file are being ignored.

// Configure the WordPress Customizer
require(get_template_directory_uri() . '/admin/customizer/customizer.php');

The above code is supposed to call a file that’s adding a function that creates sections, settings and controls in the WordPress Customizer. When the function is placed directly into my functions.php file everything works fine but when I try to include it, it does nothing.

The included file also contains includes, I don’t know if this is of relevance.

The above code is hooked on after_setup_theme, if you think I should do anything differently please tell me, I want to learn.

Solutions Collecting From Web of "PHP files included in functions.php don't seem to work"

I always break my functions.php up into more managable, specific functionality files, for instance, all footer related functions goes into a file called footer-functions.php and pagination functions goes into a file called pagination-functions.php. This way, my code stays organised, managable and I don’t kill my functions.php. This however, is only related to theme specific functions, site specific functions goes into plugins in much more the same type of method

To come back to the point, at the end I include all my smaller specific function files into functions.php with require_once() as follow

require_once( 'footer-functions.php' );
require_once( 'pagination-functions.php' );

This is all you need. You should not hook this to any hook or add it in any function, it can have expected behavior which can lead to a broken site

Your problem is that you’ve used the get_template_directory_uri() function for your include path. As indicated by the function name, this returns a URI (not a path), which is no use for including one php file into another.

Simply enter the relative path of the file to be included.

E.g. if your structure is:

/my-theme
 |--functions.php
 |--/lib
     |--customizer.php
     |--tools.php
 |--style.css
 ...etc

In your functions.php simple put

<?php

require_once('lib/customizer.php');
require_once('lib/tools.php');

The approach is the same whether using require(), require_once() or include()

In my opinion, your functions file can be as big as you want it, but organizing functions into includes is a very logical process. I would first check that get_template_directory_uri() is returning the correct path. You can also include files without the full path within your theme if /admin/ is in your theme root: require(‘/admin/customizer/customizer.php’);